2017-10-04 136 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); 
    } 
} 
相关问题