使用以下代码将各种线添加到表示路线的地图中:
private LineLayer makeLineLayer(List<GeoPoint> routePoints, String title) {
String sourceTitle = "line-layer-" + lineCount;
List<Position> points = new ArrayList<>(routePoints.size());
List<Feature> routes = new ArrayList<>(routePoints.size());
for (GeoPoint point : routePoints) {
points.add(Position.fromCoordinates(point.getLongitude(), point.getLatitude()));
}
LineString route = LineString.fromCoordinates(points);
Feature routeFeature = Feature.fromGeometry(route);
routeFeature.addStringProperty("custom-line", "0");
routes.add(routeFeature);
GeoJsonSource linesSource = new GeoJsonSource(
sourceTitle,
FeatureCollection.fromFeatures(routes));
mapboxMap.addSource(linesSource);
LineLayer lineLayer = new LineLayer(title, sourceTitle);
lineLayer.setProperties(
//Sets properties...
);
return lineLayer;
}
LineLayer lineLayer = makeLineLayer(getRoutePoints());
mapboxMap.addLayer(lineLayer);
我希望能够确定何时单击这些行之一。目前,MapBox 调用OnMapClick
并传入一个LatLng
对象。然后,我可以使用以下命令查询具有该custom-line
属性的渲染功能:
PointF pixel = mapboxMap.getProjection().toScreenLocation(point);
List<Feature> selectedKeys = mapboxMap.queryRenderedFeatures(pixel, Filter.has("custom-line"));
如果selectedKeys
then 包含任何返回Feature
的对象,我可以使用 查询它们的坐标.getGeometry()
。将这些值与LatLng
传入的对象中的值进行比较,可以粗略估计点击了哪条线。但是,当行项目紧密分组时,这是非常不准确和麻烦的。
如何监听这些订单项的点击事件?