Android Maps V2 API Center Current location on screen

I am trying to complete this simple task, which seemed pretty simple, but it still does not work the way it should have done. I use the method below, but the phone scales in the center of the city, about 2 km from where my blue dot is.

public void setUpMap() {
        mGoogleMap.setMyLocationEnabled(true);

        String context = Context.LOCATION_SERVICE;

        LocationManager locationManager = (LocationManager)mContext.getSystemService(context);

        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location myLocation = locationManager.getLastKnownLocation(provider);

        double latitude = myLocation.getLatitude();
        double longitude = myLocation.getLongitude();

        LatLng latLng = new LatLng(latitude, longitude);

        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));


    }

I tried to set different scaling values, but nothing changes. I'm not sure if I should create newLatLngBounds or not, as everyone says this is the right way. It seems I am not getting my current location, but my location is in the city.

+4
source share
2 answers

new Answer: -

mMap.setMyLocationEnabled(true);

                Location myLocation = mMap.getMyLocation();

                if (myLocation != null) {
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                            new LatLng(myLocation.getLatitude(), myLocation
                                    .getLongitude()), 14));
                }
+2

, .

FusedLocation API

FusedLocation API (. App Appware), . "" , GoogleMap.

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
     mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(location.getLatitude(), location.getLongitude()), 15));
}

OnMyLocationChangeListener

GoogleMap OnMyLocationChangeListener, :

mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(location.getLatitude(), location.getLongitude()), 15));
            mMap.setOnMyLocationChangeListener(null);
        }
    });

, , Google FusedLocation API. , , - API, .

+1

Source: https://habr.com/ru/post/1546318/


All Articles