获取当前位置lat和lng在地图就绪

时间:2017-10-04 07:45:51

标签: java android

我想获取当前位置并将相机移动到当前位置,然后将当前位置(LatLng)保存到我的数据库

我获得了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 log cat:

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

2 个答案:

答案 0 :(得分:2)

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 :(得分: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);
    }
}