2013-10-27 57 views
15

我剛剛得到這個工作,但當我關閉我的Wifi,看看我是否可以使用我的GPS獲得更準確的位置,它現在給我帶來了一個NullPointer錯誤,我看不出爲什麼。getLastKnownLocation returns NULL

下面的代碼被首先調用;

public void onConnected(Bundle dataBundle) { 
     connected = true; 
     //Request an update from the location client to get current Location details 
     mLocationClient.requestLocationUpdates(mLocationRequest,this); 
     Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); 
    } 

然後需要這種方法被稱爲

public void onLocationChanged(android.location.Location location) { 
     // Report to the UI that the location was updated 
     String msg = "Updated Location: " + 
       Double.toString(location.getLatitude()) + "," + 
       Double.toString(location.getLongitude()); 
     if(tmp != location.getLatitude()) 
     { 
      Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 
     } 
     tmp = location.getLatitude(); 
    } 

你可以看到我有2個烤麪包那裏,他們回來精細與實際GPS座標,但是當我把下面的代碼崩潰了上新的位置線getLastKnownLocation

public String getLocation() 
    { 
     // Get the location manager 
     LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     String bestProvider = locationManager.getBestProvider(criteria, false); 
     android.location.Location location = locationManager.getLastKnownLocation(bestProvider); 
     Double lat,lon; 
     DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
     Date date = new Date(); 
     try { 
      lat = location.getLatitude(); 
      lon = location.getLongitude(); 
      Toast.makeText(this,""+lat.toString()+"-"+lon.toString(),Toast.LENGTH_SHORT).show(); 
      return new LatLng(lat, lon).toString(); 
     } 
     catch (NullPointerException e){ 
      Toast.makeText(this,"HELL-NO",Toast.LENGTH_SHORT).show(); 
      Log.e("HELL-NO","n",e); 
      e.printStackTrace(); 
      return null; 
     } 
    } 

我只是不知道爲什麼,當我嘗試檢索位置,這似乎已在被檢索方法,它只是提出一個空指針。

+0

你混合新的API和老的getLastLocation遵循這一 https://stackoverflow.com/a/48033659/4997704 –

回答

27

我只是改變

locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) to 
locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 

這解決了這個解決方案給我。完整的代碼片段:

map = ((MapFragment) getFragmentManager(). 
      findFragmentById(R.id.map)).getMap(); 

    map.setMyLocationEnabled(true); 

    locationManager = (LocationManager)getSystemService 
      (Context.LOCATION_SERVICE); 
    getLastLocation = locationManager.getLastKnownLocation 
      (LocationManager.PASSIVE_PROVIDER); 
    currentLongitude = getLastLocation.getLongitude(); 
    currentLatitude = getLastLocation.getLatitude(); 

    currentLocation = new LatLng(currentLatitude, currentLongitude); 

希望這會有所幫助。

+2

它適用於我,但GPS_PROVIDER和PASSIVE_PROVIDER之間的區別是什麼? –

+0

@aishwatsingh http://stackoverflow.com/a/5655329/3878508 –

+0

@TroyZuroske任何想法爲什麼被動提供者修復這個問題?對不起,碰撞。爲我工作,謝謝! –

0

我一直在努力與這一個很長一段時間。但是Google剛剛提出了一個解決方案。

googleMap.getMyLocation(); 

獲取api V2上的藍點位置。

0

試試這個代碼:

LocationManager mLocationManager; 
Location myLocation = getLastKnownLocation(); 

private Location getLastKnownLocation() { 
    mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE); 
    List<String> providers = mLocationManager.getProviders(true); 
    Location bestLocation = null; 
    for (String provider : providers) { 
     Location l = mLocationManager.getLastKnownLocation(provider); 
     if (l == null) { 
      continue; 
     } 
     if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) { 
      // Found best last known location: %s", l); 
      bestLocation = l; 
     } 
    } 
    return bestLocation; 
} 
相關問題