【问题标题】:Rotate and change position for markers in latest MapBox SDK 6.7在最新的 MapBox SDK 6.7 中旋转和更改标记的位置
【发布时间】:2019-04-30 05:43:15
【问题描述】:

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 来实现这一点?

【问题讨论】:

    标签: mapbox mapbox-android


    【解决方案1】:

    这可以通过最新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
            }
        });
    

    【讨论】:

    • 大家请注意,这已经是自 SDK 7 以来过时的示例
    • 最好看官方网站。类似于 SymbolOptions carOptions = new SymbolOptions().withIconRotate(90f).
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    相关资源
    最近更新 更多