How to get user's current location and location updates on Yandex MapKit 3.4.0

5.1k Views Asked by At

I'm using Yandex MapKit in my application. Currently I need to get users current location and animate camera on that location. The problem is the documentation is really poor and most of the answers on the internet seem to be deprecated on latest version of map. How can I get the current location and location updates?

3

There are 3 best solutions below

0
Azamat Mahkamov On

I have found one simple solution. The map kit 3.0.4 seems to have updated its api methods.

        MapKit mapKit = MapKitFactory.getInstance();

    mapKit.createLocationManager().requestSingleUpdate(new LocationListener() {
        @Override
        public void onLocationUpdated(@NonNull Location location) {
            Log.d("TagCheck", "LocationUpdated " + location.getPosition().getLongitude());
            Log.d("TagCheck", "LocationUpdated " + location.getPosition().getLatitude());
            mapView.getMap().move(
                    new CameraPosition(location.getPosition(), 14.0f, 0.0f, 0.0f),
                    new Animation(Animation.Type.SMOOTH, 1),
                    null);

        }

        @Override
        public void onLocationStatusUpdated(@NonNull LocationStatus locationStatus) {

        }
    });

this code will trigger device location.

dont forget to include MapKitFactory.setApiKey(MAPKIT_API_KEY); and MapKitFactory.initialize(this); before setContentview or returning view in fragments.

2
Jasurbek Oripov On

First you need to get permisson for Location !!!

var fusedLocationClient =LocationServices.getFusedLocationProviderClient(this@MapActivity)

fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? ->

    Log.d(TAG, "getUserLocation:  $location.latitude")

    Log.d(TAG, "getUserLocation:  $location.longitude")

    }
}
0
Arty Morris On

The answer provided above is incorrect, it allows you to find out the location only once. You need to use a different code

mapKit = MapKitFactory.getInstance();
mapKit.createLocationManager().subscribeForLocationUpdates(0,0, 0, true, FilteringMode.ON, new LocationListener() {
        @Override
        public void onLocationUpdated(@NonNull Location location) {
            Log.d("TagCheck", "LocationUpdated " + location.getPosition().getLongitude());
            Log.d("TagCheck", "LocationUpdated " + location.getPosition().getLatitude());
        }

        @Override
        public void onLocationStatusUpdated(@NonNull LocationStatus locationStatus) {
        }
});