0

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 View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.location_layout, container, false);

    checkMap();
    return view;
}

private void checkMap() {
    if (mMap == null) {
        /*Try to obtain the map from the SupportMapFragment*/
        mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        /*Check if we were successful in obtaining the map.*/
        if (mMap != null) {
            initMap();
        }
    }
}

private void initMap() {
    LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, true);
    /*Get last known location*/
    defaultLocation = locationManager.getLastKnownLocation(provider);
    /*If it doesn't exist, update once*/
    if (defaultLocation == null) {
        locationManager.requestSingleUpdate(criteria, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                defaultLocation = location;
            }
            @Override
            public void onProviderEnabled(String provider) {
            }
            @Override
            public void onProviderDisabled(String provider) {
            }
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        }, getActivity().getMainLooper());
    }
    /*When we get the location, focus the map on it*/
    if (defaultLocation != null) {
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(defaultLocation.getLatitude(), defaultLocation.getLongitude()), 5));
    }
}

I'm not getting an issue, it just doesn't focus the map on the current location. What I'm doing wrong?

Manifest:

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- The following two permissions are not required to use
    Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

<meta-data 
    android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version" />       
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="...KEY..."/> 
masmic
  • 3,526
  • 11
  • 52
  • 105

3 Answers3

1

Try Below Code:

Criteria criteria = new Criteria();
        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        List<String> providers = locationManager.getProviders(true);
        for (String provider : providers) {
            locationManager.requestLocationUpdates(provider, 1000, 0,
                    new LocationListener() {
                        public void onLocationChanged(Location location) {
                        }

                        public void onProviderDisabled(String provider) {
                        }

                        public void onProviderEnabled(String provider) {
                        }

                        public void onStatusChanged(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( new LatLng( lat, lng), 15));
            // Zoom in, animating the camera.
         googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 1500, null);
}
}

it work for me. Thats it...

Jayesh Khasatiya
  • 2,140
  • 1
  • 14
  • 15
0

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

LatLng latLng = new LatLng(latitude, longitude); // put your lat long here

CameraUpdate center = CameraUpdateFactory.newLatLng(latLng);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);

googleMap.moveCamera(center);
googleMap.animateCamera(zoom);
Ashish Tiwari
  • 2,168
  • 4
  • 30
  • 54
0

Change

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

To inside the requestSingleUpdate listener:

        @Override
        public void onLocationChanged(Location location) {
            defaultLocation = location;
             mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(defaultLocation.getLatitude(), defaultLocation.getLongitude()), 5));
        }
Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82