You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Add OnPolygonClickListener and OnPolylineClickListener #3720
Hi, first, I'm very happy with the Mapbox products in general!
I'm just implementing the new Android library and I was wondering if there is a way to add a listener to clicks on Polylines. I know this is posible in de js API, but I can't find how to do this in Android.
This would be to change their style and show more information on them.
Thank you reaching out and requesting this feature, currently we do not expose this as an API but is something we are thinking about adding in future releases.
And change the way the following method is called and interacted with. This method should be generalised in handling all types of annotations and the underlying native method should only be called once.
privateList<Marker> getMarkersInBounds(@NonNullBoundingBoxbbox) {
if (bbox == null) {
Log.w(TAG, "bbox was null, so just returning null");
returnnull;
}
// TODO: filter in JNI using C++ parameter to getAnnotationsInBoundslong[] ids = mNativeMapView.getAnnotationsInBounds(bbox);
List<Long> idsList = newArrayList<>(ids.length);
for (inti = 0; i < ids.length; i++) {
idsList.add(ids[i]);
}
List<Marker> annotations = newArrayList<>(ids.length);
intcount = mAnnotations.size();
for (inti = 0; i < count; i++) {
Annotationannotation = mAnnotations.get(i);
if (annotationinstanceofMarker && idsList.contains(annotation.getId())) {
annotations.add((Marker) annotation);
}
}
returnnewArrayList<>(annotations);
}
As indicated in the TODO we should consider moving some things into C++
changed the title [-]Add OnPolygonClickListener amd OnPolylineClickListener[/-][+]Add OnPolygonClickListener and OnPolylineClickListener[/+]on Jan 28, 2016
I'm currently looking at implementing this but it seems that our native method getAnnotationsInBounds is not working for polylines. Need to check for polygon.
Digging a bit deeper, I'm noticing that getAnnotationInBounds is calling a native method called getPointAnnotationsInBounds so this means getAnnotationInBounds will not work for Shape annotations..
I will have to postpone further integration until #3990 has been resolved.
Going to comment the interface definitions because I want to PR the progress made for #3176.
With #5165 now merged, we can think again of bringing this feature to the Android SDK (the iOS equivalent ticket is tracked in #2082).
At this point we're strongly focused on shipping v4.2 of the SDK (we're getting close) so I don't think this feature will make it on time, but we'll update this ticket as soon as we make any progress.
Thanks a lot @tobrun , I am new to mapbox and am developing a map with multiple polygons, and have lots of info in properties and are these properties I need to get, with setOnPolygonClickListener I can not get this information, what can you suggest me?
For that you can better use a geojson source with a filllayer. To react to clicks, you override onmapclicklistener and call into mapboxmap#queryRenderedFeatures with the Id of your layer.
HI @tobrun,
I am adding multiple polygons and also I need the clicklistner for the same.
As you suggested earlier to @igor-nm I have created filllayer for multiple polygons. Code shown below :
public void addPolygonToMap(List<List> polygonsPosition, String layerName, int color) {
Feature polygonFeature = Feature.fromGeometry(com.mapbox.services.commons.geojson.Polygon.fromCoordinates(polygonsPosition));
final GeoJsonSource source = new GeoJsonSource(layerName,
FeatureCollection.fromFeatures(new Feature[]{polygonFeature}), new GeoJsonOptions());
mMapboxMap.addSource(source);
FillLayer fillLayer = new FillLayer(layerName, layerName);
fillLayer.setProperties(
PropertyFactory.fillColor(color)
);
mMapboxMap.addLayer(fillLayer);
}
While in OnMapclickListner (code shown below) I am getting all the coordinates of that layer. While I need the only polygon which user has clicked. Please suggest me how should I do it.
@viveksaruk you can add an unique id as feature property to your features and reference that later in your code to make a distinction to determine which one was clicked.
Activity
tobrun commentedon Jan 28, 2016
@buzoherbert
Thank you reaching out and requesting this feature, currently we do not expose this as an API but is something we are thinking about adding in future releases.
@bleege @zugaldia:
Google maps SDK exposes:

To make this possible we should refactor
SingleTapConfirmed, it now only takes Markers in account:And change the way the following method is called and interacted with. This method should be generalised in handling all types of annotations and the underlying native method should only be called once.
As indicated in the TODO we should consider moving some things into C++
[-]Listener for Polyline/Polygon tap[/-][+]Add OnPolygonClickListener amd OnPolylineClickListener[/+][-]Add OnPolygonClickListener amd OnPolylineClickListener[/-][+]Add OnPolygonClickListener and OnPolylineClickListener[/+]buzoherbert commentedon Jan 28, 2016
@tobrun Thank you so much for being so responsive. I'll keep an eye on this thread!
tobrun commentedon Feb 17, 2016
I'm currently looking at implementing this but it seems that our native method getAnnotationsInBounds is not working for polylines. Need to check for polygon.
update neither for polygons
tobrun commentedon Feb 17, 2016
For example,
getAnnotationsInBoundsis returning 0 in following cases:tobrun commentedon Feb 17, 2016
Digging a bit deeper, I'm noticing that
getAnnotationInBoundsis calling a native method calledgetPointAnnotationsInBoundsso this means getAnnotationInBounds will not work for Shape annotations..tobrun commentedon Feb 17, 2016
I will have to postpone further integration until #3990 has been resolved.
Going to comment the interface definitions because I want to PR the progress made for #3176.
7 remaining items
zugaldia commentedon Oct 3, 2016
With #5165 now merged, we can think again of bringing this feature to the Android SDK (the iOS equivalent ticket is tracked in #2082).
At this point we're strongly focused on shipping v4.2 of the SDK (we're getting close) so I don't think this feature will make it on time, but we'll update this ticket as soon as we make any progress.
Mukeyii commentedon Dec 10, 2016
I solved this with a little workaround:
You can use this class in any activity together with the onMapClickedListener, for example like this:
igor-martucelli commentedon Nov 1, 2017
Hi folks, does anyone know got any news on this?
tobrun commentedon Nov 1, 2017
@igor-nm this is implemented and available in 5.1.5 and 5.2.0-beta.3
igor-martucelli commentedon Nov 3, 2017
Thanks a lot @tobrun , I am new to mapbox and am developing a map with multiple polygons, and have lots of info in properties and are these properties I need to get, with setOnPolygonClickListener I can not get this information, what can you suggest me?
tobrun commentedon Nov 3, 2017
For that you can better use a geojson source with a filllayer. To react to clicks, you override onmapclicklistener and call into mapboxmap#queryRenderedFeatures with the Id of your layer.
viveksaruk commentedon Feb 8, 2018
HI @tobrun,
I am adding multiple polygons and also I need the clicklistner for the same.
As you suggested earlier to @igor-nm I have created filllayer for multiple polygons. Code shown below :
public void addPolygonToMap(List<List> polygonsPosition, String layerName, int color) {
While in OnMapclickListner (code shown below) I am getting all the coordinates of that layer. While I need the only polygon which user has clicked. Please suggest me how should I do it.
tobrun commentedon Feb 8, 2018
@viveksaruk you can add an unique id as feature property to your features and reference that later in your code to make a distinction to determine which one was clicked.
viveksaruk commentedon Feb 9, 2018
Thanks @tobrun for quick reply.It worked for me. But How could I group multiple Features with unique ID in one Layer?