在 mapbox 中,单击 android 上的按钮时如何 return 我的位置

In mapbox , how can I return my location when I click the button on android

点击按钮时如何return我的位置,就像google地图一样。 我想单击一个按钮并在地图框中显示我的位置。 我该怎么办?

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

Mapbox Android 演示应用程序中还有一个基本的 LocationComponent 示例: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() 的另一种选择。