2015-09-04 70 views
0

我正在開發一個基於地圖視圖的應用程序,獲取經度和緯度,並在地圖上顯示用於繪製路線的圖表。當我在路上嘗試應用程序時(使用自行車),那麼得到錯誤的經緯度就意味着像循環一樣獲得方向和路線。如何在Android應用上獲取精確的經度和緯度座標?

我想要精確的Android應用程序的緯度和經度座標。

我用於定位的波紋碼。

public Location getLocation() { 
    try { 
     locationManager = (LocationManager) this.getApplicationContext().getSystemService(LOCATION_SERVICE); 
     // getting GPS status 
     isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     // getting network status 
     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
     if (!isGPSEnabled && !isNetworkEnabled) { 
      Toast.makeText(this, "Youe GPS service is not enabled",Toast.LENGTH_SHORT).show(); 
     } else { 
       if (isNetworkEnabled) { 
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,GPS_TIME_INTERVAL,GPS_DISTANCE, this); 
       Log.d("Network", "Network"); 
       if (locationManager != null) { 
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 
       } 
      else if (isGPSEnabled) { 
        if (location == null) { 
         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,GPS_TIME_INTERVAL,GPS_DISTANCE, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      if(location != null) 
      persistLocation(location); 
      stopUsingGPS(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return location; 
} 
+0

你可以調用:'requestLocationUpdates',但是你沒有顯示那個回調的代碼。這就是準確讀數的來源。 – weston

+0

此外,爲什麼你更喜歡GPS上的網絡位置?並閱讀:http://developer.android.com/guide/topics/location/strategies.html你不需要身體離開房子來測試你的應用程序。 – weston

+0

@Lucky,你使用谷歌地圖嗎? –

回答