使Mapbox标记可点击,并返回标记的ID

时间:2019-06-12 20:56:39

标签: java android mapbox mapbox-android

因此,我制作了一个MapActivity来显示MapBox地图。

然后我从数据库 中将所有标记(引脚,最终是具有经度和纬度的Point对象)加载到列表 markerCoordinates中。然后通过GeoJsonSource()给出markerCoordinates。

现在我的所有标记都显示在地图上。

现在我不知道如何使标记可点击,单击时我需要获取标记的ID,以便稍后可以重定向到另一个Activity,然后加载全部基于该ID的数据。引脚的ID必须与数据库中的ID相同。

Mapbox文档并不能真正帮助我解决这个问题。

有人知道如何实现吗?

MapActivity.java

public class MapActivity extends AppCompatActivity {
    private MapView mapView;
    DatabaseHelper myDb;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_moments:
                    Intent intent = new Intent(MapActivity.this, MapActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivity(intent);
                    break;

                case R.id.navigation_addmoment:
                    Intent intent2 = new Intent(MapActivity.this, AddMomentActivity.class);
                    intent2.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivity(intent2);
                    break;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myDb = new DatabaseHelper(this);

    Mapbox.getInstance(this, "pk.eyJ1IjoiaGlqYWNrbWFuaWFjIiwiYSI6ImNqdWlyb3E3NzE5bjc0Zm9lOHpta3AzajMifQ.ithKXW2RvhRzlPqXWBexyg");

    setContentView(R.layout.activity_map);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    mapView = findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {

        @Override
        public void onMapReady(@NonNull MapboxMap mapboxMap) {
            mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
                @Override
                public void onStyleLoaded(@NonNull Style style) {

                    // Importing markers from database
                    List<Feature> markerCoordinates = new ArrayList<>();

                    loadPins(markerCoordinates);

                    // Adding markers on the map
                    style.addSource(new GeoJsonSource("source-id",
                            FeatureCollection.fromFeatures(markerCoordinates)));

                    // Some styling below
                    style.addImage("marker-icon-id",
                            BitmapFactory.decodeResource(
                                    MapActivity.this.getResources(), R.drawable.mapbox_marker_icon_default));

                    SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id");
                    symbolLayer.withProperties(
                            PropertyFactory.iconImage("marker-icon-id")
                    );
                    style.addLayer(symbolLayer);
                }
            });
        }
    });
}

// Switch from map to moments
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.header_items, menu);
    final MenuItem listItem = menu.findItem(R.id.list_menu);
    listItem.setVisible(true);

    listItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(MapActivity.this, MomentsActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
            return true;
        }
    });

    return true;
}

// Reading the Database, appending each row to allMarkers
public void loadPins(List<Feature> allMarkers) {
    Cursor res = myDb.getAllData();
    if(res.getCount() == 0) {
        // Error message
        return;
    }

    while(res.moveToNext()) {
        double longitude = Double.parseDouble(res.getString(4));
        double latitude = Double.parseDouble(res.getString(5));

        if(longitude == 0 || latitude == 0) {
            continue;
        } else {
            allMarkers.add(Feature.fromGeometry(
                    Point.fromLngLat(
                            longitude,
                            latitude
                    )
            ));
        }
    }
}

@Override
public void onStart() {
    super.onStart();
    mapView.onStart();
}

@Override
public void onResume() {
    super.onResume();
    mapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mapView.onPause();
}

@Override
public void onStop() {
    super.onStop();
    mapView.onStop();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}

}

1 个答案:

答案 0 :(得分:0)

首先,您应该实现onMapClickonMapLongClick方法,以便处理所有地图单击并检索所需的数据。
每次单击获取标记ID的步骤大致如下:

  • 检查单击是在标记上还是在其他位置。

    您可以通过查询源图层(您的GeoJsonSource)并检查单击的点附近是否有要素来实现。

  • 如果找到了功能,请获取所需的属性。

    您可以使用feature.getProperties()获取所有属性,或使用feature.getStringProperty("name_of_the_property")获取特定属性。查看更多here

选中此example,以了解如何实现所有这些功能。