添加的标记不可见-Mapbox 6.7.0

时间:2018-12-13 06:14:16

标签: mapbox mapbox-android

在尝试使用符号图层将标记添加到mapbox时,标记不可见。 试图添加标记,如下所示,

mapView.getMapAsync(m -> {
        isMapReady = true;
        mapboxMap = m;
        mapboxMap.addOnMapClickListener(MapViewFragment.this);

 addMarkerToSymbolLayer(45);
 updateCameraPosition(location); });

private void updateCameraPosition(Location location){

  if (mapboxMap != null) {
        LatLng latLong = new LatLng();
        if (location != null) {
            latLong.setLatitude(location.getLatitude());
            latLong.setLongitude(location.getLongitude());
        }
        final CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(latLong)
                .build();
        mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        mapView.postDelayed(() -> mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), AppConstants.MAP_CAMERA_ANIMATE_DURATION_MS_5000), 100);

private void addMarkerToSymbolLayer(float headDirection) {

    GeoJsonSource geoJsonSource = new GeoJsonSource("geojson-source",
    Feature.fromGeometry(Point.fromLngLat(77.6387, 12.9610)));
    mapboxMap.addSource(geoJsonSource);

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

    mapboxMap.addImage("compass-needle-image-id", compassNeedleSymbolLayerIcon);


    SymbolLayer aircraftLayer = new SymbolLayer("aircraft-layer", "geojson-source")
            .withProperties(
                    PropertyFactory.iconImage("compass-needle-image-id"),
                    PropertyFactory.iconRotate(headDirection),
                    PropertyFactory.iconIgnorePlacement(true),
                    PropertyFactory.iconAllowOverlap(true)
            );
    mapboxMap.addLayer(aircraftLayer);

}

如果在移动相机后调用addMarkerToSymbolLayer(),则标记可见。为什么添加标记取决于相机位置?我必须多次移动相机以满足我的要求。该如何处理?

在不推荐使用的MarkerView和MarkerViewOptions中,添加标记时我也没有任何问题。

我注意到,如果在调用函数addMarkerToSymbolLayer(45)时给定100ms的延迟,则该标记可见,并且一切正常!

1 个答案:

答案 0 :(得分:2)

onMapReady()甚至在加载所有样式之前就被调用。在添加符号层之前,必须加载所有样式。添加以下侦听器并要求在其中添加标记。

mapView.addOnDidFinishLoadingStyleListener(new MapView.OnDidFinishLoadingStyleListener() {
        @Override
        public void onDidFinishLoadingStyle() {

        }
    });