2013-07-25 59 views
-3

我正在開發android應用程序。我困在問題中。 getLastKnownLocation(provider)返回null Android版本4.1.1,而其他版本是好的。供應商已啓用,其餘都可以。我不知道問題在哪裏。這是代碼。Android LocationManager.getLastKnownLocation()在Android 4.1.1上返回null

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    String provider = locationManager.getBestProvider(criteria, true); 
    location = locationManager.getLastKnownLocation(provider); 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
    LatLng latLng = new LatLng(latitude, longitude); 
    mMap.clear(); 
    System.out.println("Current Location = "+latLng); 

回答

0

locationManager.getLastKnownLocation將返回null如果供應商被禁止,這裏是文檔

http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)

+0

感謝您的回答,但供應商已啓用。它也返回以前的位置爲其他Android版本如4.2或等 – user2576232

+0

但getLastKnownLocation不保證返回位置,如果它爲空,那麼你應該找到當前位置 –

+0

這就是我想解決的問題。我想獲得當前位置,有沒有其他方法可以用於相同? – user2576232

0

嘗試此其工作對我來說...

mGoogleMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() { 

        @Override 
        public boolean onMyLocationButtonClick() 
         { 

          Location myLocation = mGoogleMap.getMyLocation(); 
          onLocationChanged(myLocation); 
          return false; 
         } 
       }); 
相關問題