如何在 yandex mapkit 中获取我当前位置的坐标?

Posted

技术标签:

【中文标题】如何在 yandex mapkit 中获取我当前位置的坐标?【英文标题】:How do I get the coordinates of my current location in yandex mapkit? 【发布时间】:2020-02-05 13:22:20 【问题描述】:

网上资料很少,请大家帮忙,能有项目吗?

【问题讨论】:

你尝试使用mapkit LocationManager tech.yandex.ru/maps/mapkit/doc/3.x/concepts/android/mapkit/ref/… 吗? 请举例说明如何使用。 【参考方案1】:
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);
    

【讨论】:

以上是关于如何在 yandex mapkit 中获取我当前位置的坐标?的主要内容,如果未能解决你的问题,请参考以下文章

Yandex MapKit 设置标签区域设置

检查位置图层是不是已更改。 Yandex MapKit 3.0

如何在 android 中添加 Yandex 地图?

Yandex MapKit iOS:集群和地标

MapKit JS - 如何获取我的当前位置

使用 SwiftUI 和 MapKit 检索用户当前位置