2016-05-24 54 views
2

我有一個代碼應該返回用戶位置,但經度不斷返回null,結果我的應用程序不斷崩潰。我如何防止這種情況?LocationManager通過Android中的getLastKnownLocation返回null經度

//get the user longitude and latitude 
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
double longitude = location.getLongitude(); 
double latitude = location.getLatitude(); 

Geocoder geocoder; //return meaningful data from user location 
List<Address> addresses; 
geocoder = new Geocoder(this, Locale.getDefault()); 
addresses = geocoder.getFromLocation(latitude, longitude, 1); 
address = addresses.get(0).getAddressLine(0); 
city = addresses.get(0).getLocality(); 
+0

請發佈您的錯誤堆棧跟蹤 – Prudhvi

+0

double longitude = location.getLongitude(); – Blaze

+2

你的位置是空的,而不是你的經度。 – Zircon

回答

7

添加支票。

if (location != null) { 
    latitude = location.getLatitude(); 
    longitude = location.getLongitude(); 
} 

至於爲什麼它沒有返回值,你需要添加一些日誌記錄。

更完整的解決方案試試這個:

public Location getLocation() { 
    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; 
      // First get destination from Network Provider 
      if (isNetworkEnabled) { 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
       // Log.d("Network", "Network"); 
       if (locationManager != null) { 
        location = locationManager.getLastKnownLocation(
          LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 
      } 
      // 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); 
        // Log.d("GPS Enabled", "GPS Enabled"); 
        if (locationManager != null) { 
         location = locationManager.getLastKnownLocation(
           LocationManager.GPS_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return location; 
} 

在此實現,這是從一個類的方法,我從我的活動調用。隨着代碼需要釋放資源後,他們不再需要。

例如:

/** 
* Stop using GPS listener 
* Calling this function will stop using GPS in your app 
*/ 
public void stopUsingGPS() { 
    if (locationManager != null) { 
     locationManager.removeUpdates(LocFinder.this); 
    } 
} 

免責聲明請注意,這是需要的代碼中的一部分,並且在實施定位服務,以協助。資源在不被使用時需要被釋放,位置服務的使用與項目的所有其他方面一樣需要在整個應用程序的流程中進行管理。

查看Getting the Last Known LocationAndroid Location API - TutorialAndroid - Location Based Services以更全面地實施位置服務。

+0

我有這條線上的錯誤locationManager =(LocationManager)mContext – Blaze

+0

@Blaze你抱怨無經度,我幫你避免這種情況。我會找到一個教程的鏈接來幫助你。掛在 –

+0

哦來吧downvoter,這是什麼問題? –

相關問題