Android 如何擷取經緯度

來源:互聯網
上載者:User

標籤:

移動是手機與手持功能的最大特點,可以通過Eclipse的DDMS視圖,類比裝置的位置變化,改變經緯度後,點擊send,然後運行程式,在應用程式中,動態擷取裝置位置,然後顯示當前的位置資訊。
  
  擷取位置資訊分為三步:
  
  1.添加系統許可權,來支援對LBS硬體的訪問
  
Java代碼:

< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">< /uses-permission>

 

  2.得到系統服務的LocationManager對象
  
Java代碼:

LocationManager loctionManager;String contextService=Context.LOCATION_SERVICE;//通過系統服務,取得LocationManager對象loctionManager=(LocationManager) getSystemService(contextService);

       3. 得到位置提供器,通過位置提供器,得到位置資訊,可以指定具體的位置提供器,也可以提供一個標準集合,讓系統根據 標準匹配最適合的位置提供器,位置資訊是由位置提供其提供的。

  a. 通過GPS位置提供器獲得位置(指定具體的位置提供器)

Java代碼:

String provider=LocationManager.GPS_PROVIDER;Location location = loctionManager.getLastKnownLocation(provider);

       b. 使用標準集合,讓系統自動選擇可用的最佳位置提供器,提供位置

Java代碼:

Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度criteria.setAltitudeRequired(false);//不要求海拔criteria.setBearingRequired(false);//不要求方位criteria.setCostAllowed(true);//允許有花費criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗//從可用的位置提供器中,匹配以上標準的最佳提供器String provider = loctionManager.getBestProvider(criteria, true);//獲得最後一次變化的位置Location location = loctionManager.getLastKnownLocation(provider);


        最後將位置資訊顯示在TextView中,

 

       監聽位置的變化

  //監聽位置變化,2秒一次,距離10米以上
Java代碼:

loctionManager.requestLocationUpdates(provider, 2000, 10, locationListener);//位置監聽器private final LocationListener locationListener = new LocationListener() {@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}@Overridepublic void onProviderEnabled(String provider) {}@Overridepublic void onProviderDisabled(String provider) {}//當位置變化時觸發@Overridepublic void onLocationChanged(Location location) {//使用新的location更新TextView顯示updateWithNewLocation(location);}};

 

Android 如何擷取經緯度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.