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
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)