首页 > 解决方案 > Android:标记以超延迟添加到地图框

问题描述

我在我的 android 应用程序中使用地图框。初始化地图框后,我想在长按地图框时添加标记,因此按照官方网站的顺序,我将markerview依赖项添加到应用程序gradle:

dependencies {
  implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-markerview-v7:0.2.0'
}

然后我实现MapboxMap.OnMapLongClickListener并覆盖onMapLongClick.

当 mapview 准备好时,我启用enableLocationComponent并创建markerViewManager和设置 map long click listener

 mapView.getMapAsync(mapboxMap -> {
            this.mapboxMap = mapboxMap;
            mapboxMap.setStyle(Style.MAPBOX_STREETS, style -> {
                createCustomAnimationView();
                moveTo(home_longitude, home_latitude, home_zoom);
                enableLocationComponent();
                markerViewManager = new MarkerViewManager(mapView, mapboxMap);
                mapboxMap.addOnMapLongClickListener(this);
                createCustomAnimationView();
            });
        });

最后在onMapLongClick覆盖的方法中,我创建了一个 imageview 并添加到 markerViewManager。

@Override
public boolean onMapLongClick(@NonNull LatLng point) {
    ImageView imageView = new ImageView(requireContext());
    imageView.setLayoutParams(new RelativeLayout.LayoutParams(
            (int) Measurement.convertDpToPixel(32, requireContext()),
            (int) Measurement.convertDpToPixel(32, requireContext())));
    imageView.setImageResource(R.drawable.location_ic);
    MarkerView markerView = new MarkerView(new LatLng(point.getLatitude(), point.getLongitude()), imageView);
    markerViewManager.addMarker(markerView);
    return false;
}

当我运行应用程序并长按屏幕时:

第一个问题:location_ic出现在屏幕的顶部和左侧,一秒钟或更长时间后,图标放置在正确的位置

其他问题:当我移动地图时,这些标记保持固定并且不随地图移动,但在一秒钟或更长时间后放置在正确的位置。

我希望我已经解释清楚了,但如果你不明白我上传了一个小视频!!!
我的视频

标签: androidmapbox-android

解决方案


经过几天和googleing,我终于决定使用SymbolManager在mapbox上添加标记:

只需添加:

dependencies {
  implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v7:0.5.0'
}

到 app gradle,然后onMapReady像下面这样初始化插件:

symbolManager = new SymbolManager(mapView, mapboxMap,style);
symbolManager.setIconAllowOverlap(true);
symbolManager.setTextAllowOverlap(true);

并使用:

symbolManager.create(new SymbolOptions()
                .withLatLng(point)
                .withIconImage(IMAGE_MARKER_DEFAULT));

帮助查看这个页面这个

我希望它会有所帮助。


推荐阅读