2013-01-16 57 views
0

這是我用來查找我當前位置的代碼,但它沒有準確指出位置,它返回的位置爲9.920473,78.102423而不是9.909076,78.100758getLatitude(),getLongitude()返回錯誤的經度和緯度值

我無法弄清楚我要出錯的地方,請幫我提一些建議。

try { 
     locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); 

     // getting GPS status 
     isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     // getting network status 
     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (!isGPSEnabled && !isNetworkEnabled) { 
      // no network provider is enabled 
     } 
     else { 
      this.canGetLocation = true; 
      if (isNetworkEnabled) { 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        if (locationManager != null) { 
         location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
         Log.e("latitude", ""+latitude); 
         Log.e("longitude", ""+longitude); 
        } 
       } 
       // 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(); 
          } 
          Log.e("latitude", ""+latitude); 
          Log.e("longitude", ""+longitude); 
         } 
        } 
       } 
      } 
     } 
+1

你放大的還不夠嗎?它的遙控器假設你的GPS返回不正確的值。 – Siddharth

+0

請注意,您正在記錄網絡和gps位置 - 也許您應該在日誌中指出用戶正在看到的內容?另外,爲什麼測試呢?如果gps,你不關心網絡,否則如果網絡 - 你的粒度不會很好。 – KevinDTimm

+0

放大了嗎?對不起,我無法得到它@Siddharth,請你解釋一下。 。 。 – VIGNESH

回答

2

您首先使用的是網絡提供者,這不是很準確。如果您從網絡獲取位置信息,您的代碼將不會使用GPS位置。在這種情況下,位置的這種大的變化是可以預料的。如果您需要確切位置,請使用GPS提供商。

+0

但是我的GPS代碼在locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)中返回null;我無法找到我錯在哪裏。 – VIGNESH

+0

在這種情況下,最初使用NETWORK_PROVIDER中的最後一個已知位置,但稍後當您的應用從GPS_PROVIDER接收到更準確的位置時,請使用它。 – binW

0

嘗試使用此代碼:

   public void onLocationChanged(Location location) { 
      if (location != null) { 
      GeoPoint point = new GeoPoint(
       (int) (location.getLatitude() * 1E6), 
       (int) (location.getLongitude() * 1E6)); 

      Toast.makeText(getBaseContext(), 
       "Latitude: " + location.getLatitude() + 
       " Longitude: " + location.getLongitude(), 
       Toast.LENGTH_SHORT).show(); 

}

+0

它會做什麼? – purush