2017-10-04 38 views
0

我想獲得當前位置和移動相機的當前位置,然後保存當前位置(經緯度)到我的數據庫獲取當前位置經緯度和LNG在地圖上的準備

我得到ACCESS_FINE許可 和使用下面的代碼,但應用程序已停止工作

double lat = map.getMyLocation().getLatitude(); 
     double lng = map.getMyLocation().getLongitude(); 
LatLng cur = new LatLng(lat,lng); 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(cur, 17)); 

的Android日誌貓:

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference 
+0

你是否檢查過map.getMyLocation的值?他們可能是空:),請張貼你得到的錯誤,否則我們無法真正幫助你。 –

+0

stacktrace請 –

+0

請檢查安全權限和緯度是否爲空 – Saveen

回答

0

getMyLocation(),不建議使用此方法。 試試這個:

LocationManager locationManager = (LocationManager) 
        getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    Location location = locationManager.getLastKnownLocation(locationManager 
    .getBestProvider(criteria, false)); 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
1

map.getMyLocation()獲取當前位置不是獲得當前位置的最佳方式。您可以使用此類來查找用戶當前位置

public class LocationFinder { 
    private Timer timer; 
    private LocationManager locationManager; 
    private LocationResult locationResult; 
    private boolean gpsEnabled = false; 
    private boolean networkEnabled = false; 

    public boolean getLocation(Context context, LocationResult result) { 
     if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != 
       PackageManager.PERMISSION_GRANTED && 
       ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) 
         != PackageManager.PERMISSION_GRANTED) { 
      return false; 
     } 
     locationResult = result; 
     if (locationManager == null) 
      locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 


     try { 
      gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     } catch (Exception ex) { 
     } 
     try { 
      networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
     } catch (Exception ex) { 
     } 


     if (!gpsEnabled && !networkEnabled) 
      return false; 

     if (gpsEnabled) 

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0.0F, locationListenerGps); 
     if (networkEnabled) 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0.0F, locationListenerNetwork); 
     timer = new Timer(); 
     timer.schedule(new GetLastLocation(), 20000L); 
     return true; 
    } 

    LocationListener locationListenerGps = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      timer.cancel(); 
      locationResult.gotLocation(location); 
      locationManager.removeUpdates(this); 
      locationManager.removeUpdates(locationListenerNetwork); 
     } 

     public void onProviderDisabled(String provider) { 
     } 

     public void onProviderEnabled(String provider) { 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 
    }; 

    LocationListener locationListenerNetwork = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      timer.cancel(); 
      locationResult.gotLocation(location); 
      locationManager.removeUpdates(this); 
      locationManager.removeUpdates(locationListenerGps); 
     } 

     public void onProviderDisabled(String provider) { 
     } 

     public void onProviderEnabled(String provider) { 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 
    }; 

    class GetLastLocation extends TimerTask { 
     @Override 
     public void run() { 

      locationManager.removeUpdates(locationListenerGps); 
      locationManager.removeUpdates(locationListenerNetwork); 

      Location net_loc = null, gps_loc = null; 
      if (gpsEnabled) 
       gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      if (networkEnabled) 
       net_loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

      //if there are both values use the latest one 
      if (gps_loc != null && net_loc != null) { 
       if (gps_loc.getTime() > net_loc.getTime()) 
        locationResult.gotLocation(gps_loc); 
       else 
        locationResult.gotLocation(net_loc); 
       return; 
      } 

      if (gps_loc != null) { 
       locationResult.gotLocation(gps_loc); 
       return; 
      } 
      if (net_loc != null) { 
       locationResult.gotLocation(net_loc); 
       return; 
      } 
      locationResult.gotLocation(null); 
     } 
    } 

    public static abstract class LocationResult { 
     public abstract void gotLocation(Location location); 
    } 
} 
相關問題