Android: 标记以超延迟添加到 mapbox

Android: Markers is added to mapbox with super delay

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

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

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

当 mapview 准备就绪时,我启用 enableLocationComponent 并创建 markerViewManager 并设置地图 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 重写方法中,我制作了一个图像视图并添加到 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;
}

当我 运行 申请并长按屏幕时:

First problem: location_ic appear on the top and left of the screen and after a second or more, icon placed in right place

Other problem: When I move map, those markers stay fixed and not moved with map but after a second or more then placed in right place.

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

我目前也在使用 mapbox 中的标记。但在最新版本中它们被称为 "Symbols"。

要使用它们,请将 mapbox android sdk 依赖项添加到您的项目(来自 mapbox here ) and follow the other tutorial to use the symbol layer ( link here 的教程)。

如果您将所有标记都作为 GeoJSON 文件,您还可以将其添加到您将在 mapbox 网站上设置样式的自定义地图,然后在您的应用中使用。 (其他信息here

希望能帮到你,第一次回答别人

经过几天的谷歌搜索,我最终决定使用 SymbolManager 在 mapbox 上添加标记:

只需添加:

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

到应用 gradle 然后在 onMapReady 上初始化插件,如下所示:

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

并使用:

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

帮忙看看this page and this.

希望对您有所帮助。