如何在android中跟踪GPS位置?

时间:2013-03-11 09:52:25

标签: android gps

我有一个列出gps位置的android应用程序。 如何获得用于创建此视图的gps位置?

3 个答案:

答案 0 :(得分:1)

这可以使用位置管理器完成。 Here是如何使用它的示例,here是它的API参考。

答案 1 :(得分:1)

我有这个工作

   private void _getLocation()
  {
  // Get the location manager
LocationManager locationManager = (LocationManager)  getSystemService(LOCATION_SERVICE);
   Criteria criteria = new Criteria();
   String bestProvider = locationManager.getBestProvider(criteria, false);
  Location location = locationManager.getLastKnownLocation(bestProvider);

try {
lat = location.getLatitude ();
lon = location.getLongitude ();
}
catch (NullPointerException e){
 lat = -1.0;
 lon = -1.0;
 }

这很简单。它获得了最佳可用提供商并获得其最后的已知位置。 如果您只想使用GPS,请从此处this

答案 2 :(得分:1)

关注此tutorial。它说明了如何很好地获得GPS坐标。

打开 AndroidManifest.xml 并添加ACCESS_FINE_LOCATION(其中包括ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION)。此外,如果您要获得基于网络的位置,则还需要添加INTERNET权限。

从教程:

// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
    if (location == null) {
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                MIN_TIME_BW_UPDATES,
                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        if (locationManager != null) {
            location = locationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }
        }
    }
相关问题