javaandroidyandex-mapkit

How do I get the coordinates of my current location in yandex mapkit?


How do I get the coordinates of my current location in yandex mapkit ? There is very little information on the Internet, please help, can you have projects ?


Solution

  • public class MapActivity extends Activity {
        private static final String TAG = MainActivity.class.getSimpleName();
        private static final double DESIRED_ACCURACY = 0;
        private static final long MINIMAL_TIME = 1000;
        private static final double MINIMAL_DISTANCE = 1;
        private static final boolean USE_IN_BACKGROUND = false;
        public static final int COMFORTABLE_ZOOM_LEVEL = 18;
        private final String MAPKIT_API_KEY = "";
        private MapView mapView;
        private CoordinatorLayout rootCoordinatorLayout;
        private LocationManager locationManager;
        private LocationListener myLocationListener;
        private Point myLocation;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            MapKitFactory.setApiKey(MAPKIT_API_KEY);
            MapKitFactory.initialize(this);
            setContentView(R.layout.map);
    
            mapView = (MapView) findViewById(R.id.mapview);
    
            locationManager = MapKitFactory.getInstance().createLocationManager();
            myLocationListener = new LocationListener() {
                @Override
                public void onLocationUpdated(Location location) {
                    if (myLocation == null) {
                        moveCamera(location.getPosition(), COMFORTABLE_ZOOM_LEVEL);
                    }
                    myLocation = location.getPosition(); //this user point
                    Log.w(TAG, "my location - " + myLocation.getLatitude() + "," + myLocation.getLongitude());
                }
    
                @Override
                public void onLocationStatusUpdated(LocationStatus locationStatus) {
                    if (locationStatus == LocationStatus.NOT_AVAILABLE) {
                        System.out.println("sdncvoadsjv");
                    }
                }
            };
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            MapKitFactory.getInstance().onStart();
            mapView.onStart();
    
            subscribeToLocationUpdate();
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            MapKitFactory.getInstance().onStop();
            locationManager.unsubscribe(myLocationListener);
            mapView.onStop();
        }
    
        public void onFabCurrentLocationClick(View view) {
            if (myLocation == null) {
                return;
            }
    
            moveCamera(myLocation, COMFORTABLE_ZOOM_LEVEL);
        }
    
        private void subscribeToLocationUpdate() {
             if (locationManager != null && myLocationListener != null) {
                locationManager.subscribeForLocationUpdates(DESIRED_ACCURACY, MINIMAL_TIME, MINIMAL_DISTANCE, USE_IN_BACKGROUND, FilteringMode.OFF, myLocationListener);
            }
        }
    
        private void moveCamera(Point point, float zoom) {
            mapView.getMap().move(
                    new CameraPosition(point, zoom, 0.0f, 0.0f),
                    new Animation(Animation.Type.SMOOTH, 1),
                    null);
        }
    }