在最新的 MapBox SDK 6.7 中旋转和更改标记的位置

Rotate and change position for markers in latest MapBox SDK 6.7

Mapbox Android SDK: 6.7.0

我们正在开发的应用程序的要求是,我们必须在不同的 LatLng 位置添加多个标记,并用一些方位旋转它们。在旧的 mapbox 版本(4.2.1)中,我们可以毫无问题地做到这一点。

////Working code with MapBox SDK 4.2.1////
MarkerViewOptions markerViewOptions = new MarkerViewOptions();
        IconFactory iconFactory = IconFactory.getInstance(this);
        Icon arrowIcon = iconFactory.fromResource(R.drawable.compass_needle);
        markerViewOptions.icon(arrowIcon);
        markerViewOptions.position(new LatLng(position)).rotation((float) headDirection);
        marker = mapboxMap.addMarker(markerViewOptions);

    ////For updating////

        marker.setPosition(new LatLng(aircraftLocation));
        marker.setRotation((float) headDirection);
        mapboxMap.updateMarker(marker);

在最新的 Mapbox 更新中,MarkerView 和 MarkerViewOptions 已被弃用。我们正在尝试使用 Marker 和 MarkerOptions 实现相同的功能。但是我们无法旋转标记。

我们也尝试过使用 SymbolLayer。此处提供旋转功能,但我们无法为标记设置 LatLng 位置。

如何使用最新的SDK来实现这个?

在最新的SDK 6.7.0中可以通过符号层来实现

添加标记:

       Bitmap compassNeedleSymbolLayerIcon = BitmapFactory.decodeResource(
                getResources(), R.drawable.compass_needle);
        mapboxMap.addImage(AIRCRAFT_MARKER_ICON_ID, compassNeedleSymbolLayerIcon);

       GeoJsonSource geoJsonSource = new GeoJsonSource(GEOJSON_SOURCE_ID, Feature.fromGeometry(
                Point.fromLngLat(longitude, latitude)));
        mapboxMap.addSource(geoJsonSource);

        SymbolLayer Layer = new SymbolLayer(AIRCRAFT_LAYER_ID, GEOJSON_SOURCE_ID)
                .withProperties(
                        PropertyFactory.iconImage(AIRCRAFT_MARKER_ICON_ID),
                        PropertyFactory.iconRotate((float) headDirection),
                        PropertyFactory.iconIgnorePlacement(true),
                        PropertyFactory.iconAllowOverlap(true)
                );
        mapboxMap.addLayer(layer);

要旋转或更改标记的位置:

GeoJsonSource source = mapboxMap.getSourceAs(GEOJSON_SOURCE_ID);
            if (source != null) {
                source.setGeoJson(Feature.
                        fromGeometry(Point.fromLngLat(longitude, latitude)));
                layer.setProperties(
                        PropertyFactory.iconRotate((float) headDirection)
                );
            }

当您在 onMapReady() 回调中添加标记时,上述代码有时可能无法正常工作。因为 onMapReady() 在所有样式加载之前被调用。因此在 addOnDidFinishLoadingStyleListener() 回调中添加标记。

mapView.addOnDidFinishLoadingStyleListener(new MapView.OnDidFinishLoadingStyleListener() {
        @Override
        public void onDidFinishLoadingStyle() {
            //add marker here
        }
    });