首页 > 解决方案 > 在mapbox中,当我点击android上的按钮时如何返回我的位置

问题描述

单击按钮时如何返回我的位置,就像谷歌地图一样。我想单击一个按钮并在 mapbox 中显示我的位置。我应该怎么办?

标签: androidlocationmapbox

解决方案


您可以在LocationComponent此处添加通过说明https://docs.mapbox.com/android/maps/overview/location-component/

LocationComponentMapbox Android 演示应用中还有一个基本示例: https ://github.com/mapbox/mapbox-android-demo/blob/master/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/location/ LocationComponentActivity.java

一切设置好后,您将把 Mapbox 地图相机移动到按钮 中的最后一个已知位置onClick()

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {

        Location lastKnownLocation = mapboxMap.getLocationComponent().getLastKnownLocation();

        mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(
          new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude())));

      }
});

animateCamera()是另一种选择,而不是moveCamera()如果您愿意。


推荐阅读