Skip to content Skip to sidebar Skip to footer

Maps Doesn't Get Current Location

When I start my app which has a map on it, it should get the device's current location and focus the map to that location, but it doesn't. This is the way I do it: @Override public

Solution 1:

Try Below Code:

Criteria criteria = newCriteria();
        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = locationManager.getProviders(true);
        for (String provider : providers) {
            locationManager.requestLocationUpdates(provider, 1000, 0,
                    newLocationListener() {
                        publicvoidonLocationChanged(Location location) {
                        }

                        publicvoidonProviderDisabled(String provider) {
                        }

                        publicvoidonProviderEnabled(String provider) {
                        }

                        publicvoidonStatusChanged(String provider,
                                int status, Bundle extras) {
                        }
                    });
            Location location = locationManager.getLastKnownLocation(provider);
            if (location != null) {
                double lat = location.getLatitude();
                double lng= location.getLongitude();

            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom( newLatLng( lat, lng), 15));
            // Zoom in, animating the camera.
         googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 1500, null);
}
}

it work for me. Thats it...

Solution 2:

Check that you are getting proper latitude longitude or not, you can print latitude and longitude and Try This.

LatLnglatLng=newLatLng(latitude, longitude); // put your lat long hereCameraUpdatecenter= CameraUpdateFactory.newLatLng(latLng);
CameraUpdatezoom= CameraUpdateFactory.zoomTo(10);

googleMap.moveCamera(center);
googleMap.animateCamera(zoom);

Solution 3:

Change

if (defaultLocation != null) {
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(defaultLocation.getLatitude(), defaultLocation.getLongitude()), 5));
    }

To inside the requestSingleUpdate listener:

@OverridepublicvoidonLocationChanged(Location location) {
            defaultLocation = location;
             mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                newLatLng(defaultLocation.getLatitude(), defaultLocation.getLongitude()), 5));
        }

Post a Comment for "Maps Doesn't Get Current Location"