在MapBox上添加标记的最佳方法是什么? [英] What is the best way to add markers on MapBox?

查看:134
本文介绍了在MapBox上添加标记的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知, mapbox.addMarker()已弃用!我试图找出使用符号层

As most of you know, mapbox.addMarker() is deprecated! I tried to figure the new way to add markers which are using Symbol Layer

我正在使用此代码,它正在为我工​​作!但是每次我要添加标记时,它都会重新定义地图的样式

I'm using this code, it is working for me! but it re-define the Style of the map every time I want to add a marker

下面是代码:

 //Add Marker to the Map
    public void addMarker(@NonNull LatLng point) {
        Feature feature = Feature.fromGeometry(Point.fromLngLat(point.getLongitude(), point.getLatitude()));
        mapboxMap
                .setStyle(new Style.Builder()
                        .fromUri("mapbox://styles/mapbox/cjf4m44iw0uza2spb3q0a7s41")
                        .withImage(ICON_ID, marker)
                        .withSource(new GeoJsonSource(SOURCE_ID, feature))
                        .withLayer(new SymbolLayer(LAYER_ID, SOURCE_ID)
                                .withProperties(PropertyFactory.iconImage(ICON_ID),
                                        iconAllowOverlap(true),
                                        iconIgnorePlacement(true),
                                        iconOffset(new Float[]{0f, -9f}))));

    }

因此,我想知道在最新的MapBox SDK 8.5.0

谢谢!

推荐答案

mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
                    @Override
                    public void onStyleLoaded(@NonNull final Style style) {
                     AddSanciangkoStreet(style);
                     SanciangkoStreetMarker();
             }
  }

private void AddSanciangkoStreet(@NonNull Style style) {
        style.addImage("sanciangko-street",
                BitmapUtils.getBitmapFromDrawable(getResources().getDrawable(R.drawable.floodicon)));

        style.addSource(new GeoJsonSource("sanciangkoFlood1-source-id"));

        style.addLayer(new SymbolLayer("sanciangkoFlood1-layer-id", "sanciangkoFlood1-source-id").withProperties(
                iconImage("sanciangko-street"),
                iconIgnorePlacement(true),
                iconAllowOverlap(true),
                iconSize(1f)
        ));
}

private void SanciangkoStreetMarker() {
        if (map.getStyle() != null) {
            GeoJsonSource sanciangkoFlood1 = map.getStyle().getSourceAs("sanciangkoFlood1-source-id");
            if (sanciangkoFlood1 != null) {
                sanciangkoFlood1.setGeoJson(FeatureCollection.fromFeature(
                        Feature.fromGeometry(Point.fromLngLat(123.895801, 10.297237))
                ));
            }
        }
}

这篇关于在MapBox上添加标记的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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