如何使Mapbox标记可点击并获得其属性?

问题描述

我的主要活动是将数据库中的所有标记加载到列表中,然后将其添加到地图中。

public void onMapReady(@NonNull final MapBoxMap mapBoxMap) {

        MainActivity.this.mapBoxMap = mapBoxMap;

        StringRequest request = new StringRequest(url,new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
              try {

                    JSONArray jsonArray = new JSONArray(response);
                    List<Feature> symbolLayerIconFeatureList = new ArrayList<>();

                    for(int i = 0; i < jsonArray.length(); i++){
                        JSONObject crag = jsonArray.getJSONObject(i);

                        String nome = crag.getString("nome");
                        String tipo = crag.getString("tipo");
                        Double lng = crag.getDouble("longitudine");
                        Double lat = crag.getDouble("latitudine");

                        symbolLayerIconFeatureList.add(Feature.fromGeometry(Point.fromLngLat(lng,lat)));
                        symbolLayerIconFeatureList.get(i).addStringProperty("NAME_PROPERTY_KEY",nome);
                        symbolLayerIconFeatureList.get(i).addStringProperty("TYPE_KEY",tipo);

                    }

                    mapBoxMap.setStyle(new Style.Builder().fromUri("mapBox://styles/mapBox/streets-v11")

                            .withSource(new GeoJsonSource(SOURCE_ID,FeatureCollection.fromFeatures(symbolLayerIconFeatureList)))

                            .withLayer(new SymbolLayer(LAYER_ID,SOURCE_ID)
                                    .withProperties(
                                            iconImage(get("TYPE_KEY")),iconAllowOverlap(true),iconIgnorePlacement(true),textOffset(new Float[]{0f,-2.5f}),textIgnorePlacement(true),textAllowOverlap(true),textField(get("NAME_PROPERTY_KEY"))
                                    )
                            ),new Style.OnStyleLoaded() {
                        @Override
                        public void onStyleLoaded(@NonNull Style style) {
                            mapBoxMap.addOnMapClickListener(MainActivity.this);
                            mapBoxMap.getUiSettings().setlogoEnabled(false);
                            mapBoxMap.getUiSettings().setAttributionEnabled(false);

                            enableLocationComponent(style);

                        }
                    });
                } catch (JSONException e){
                    e.printstacktrace();
                }
            }
        },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printstacktrace();
            }
        });

        mQueue.add(request);

        mapView.addOnStyleImageMissingListener(new MapView.OnStyleImageMissingListener() {
            @Override
            public void onStyleImageMissing(@NonNull String id) {
                if(id.equals("Falesia")){
                    addImage(id,R.drawable.icona_falesia);
                }  else{
                    addImage(id,R.drawable.icon_gym);
                }
            }
        });
    }

我不知道如何使它们可点击

我看了文档和example,也许我必须实现onMapClick方法,但是我不知道其中包含什么。

有人知道如何实现它或以其他方式使其可点击吗?

谢谢。

解决方法

您可以在onMapReady回调中添加地图点击侦听器。在onMapClick方法中,您可以在选定的位置设置查询,以查看该位置的符号层中是否有任何要素。然后,您会获得可以使用的功能列表。这是我正在使用的内容的简明版本:

    private void handleMapClick(LatLng point){
        if (myMapboxMap != null){
            final PointF screenPoint = myMapboxMap.getProjection().toScreenLocation(point);
            List<Feature> features = myMapboxMap.queryRenderedFeatures(screenPoint,SYMBOL_LAYER_ID);
            if (!features.isEmpty()) {
                //do stuff with features in feature list
            }
        }
    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...