首页 > 解决方案 > 在具有功能的android studio中使用mapbox

问题描述

我在 android studio 中使用 MapBox 并在地图中显示一个点。现在我想要一个函数来获取一个 LatLng 变量作为输入并在地图上显示该点。(我想要在 onMapReady 之外有一个函数,通过调用该函数,将点作为输入发送给该函数在函数内,点会出现在地图上。)。请指导我

    private MapView mapView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

    Mapbox.getInstance(this, YOUR_MAPBOX_ACCESS_TOKEN);

    setContentView(R.layout.activity_main);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(@NonNull MapboxMap mapboxMap) {

        mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
        @Override
        public void onStyleLoaded(@NonNull Style style) {

            // Map is set up and the style has loaded. Now you can add data or make other map adjustments



        }
    });
    }
});

标签: android-studiomapboxmapbox-android

解决方案


很简单。在方法中编写以下代码onMapReady并使用此mapboxMap变量在地图上添加标记点。

LatLng latLng = new LatLng(20.5992, 72.9342);
mapboxMap.addMarker(new MarkerOptions().position(latLng).setTitle("set title of marker point"))

推荐阅读