旋转和更改最新MapBox SDK 6.7中标记的位置 [英] Rotate and change position for markers in latest MapBox SDK 6.7

查看:665
本文介绍了旋转和更改最新MapBox SDK 6.7中标记的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mapbox Android SDK: 6.7.0

Mapbox Android SDK: 6.7.0

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

The requirement in the application we are developing is that we have to add multiple markers in different LatLng positions and also rotate them with some bearing. In the old mapbox version(4.2.1), we could do it without any issues.

////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实现相同的功能.但是我们无法旋转标记.

In the latest Mapbox update, MarkerView and MarkerViewOptions is deprecated. We are trying to achieve the same functionality with Marker and MarkerOptions. But we are unable to rotate the markers.

我们还尝试使用SymbolLayer.旋转功能可在此处使用,但我们无法设置标记的LatLng位置.

We also tried using the SymbolLayer. Rotate function is available here but we are unable to set a LatLng position for a marker.

如何使用最新的SDK来实现这一目标?

How to proceed with the latest SDK to achieve this?

推荐答案

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

This can be achieved by symbol layer in the latest 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);

要旋转或更改标记的位置,请执行以下操作:

To rotate or changing the position of the marker:

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()回调中.

This above code may not work sometimes when you add the marker in the onMapReady() callback. Because onMapReady() is called before all styles are loaded. Hence add the marker in addOnDidFinishLoadingStyleListener() callback.

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

这篇关于旋转和更改最新MapBox SDK 6.7中标记的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆