Map polygon has no identifier to identify in click listener

Hi,

I am making a map with multiple markers an multiple polygons. Way Map is implemented is that you can have only 1 polygon click and marker click listener. Now in that listener handler I want to identify which polygon got clicked which cannot until I compare all Geopoints against all polygons I draw which is really ineffective. For markers I am using the caption as the identifier but polygon does not even have caption.

Thanks

Hi!

Unfortunately, our Polygon implementation doesn’t have an identifier for a polygon. But Vaadin implementation has a long id which increments when you create a new polygon. So you can retrieve this id by the following:

((PolygonDelegate) polygon).getPolygon().getId()

To identifier clicked polygon, you can store this id in HashMap with a String value. For instance:

@Inject
private MapViewer map;

private Map<Long, String> polygonNames = new HashMap<>();

@Override
public void init(Map<String, Object> params) {
    List<GeoPoint> coordinates = PolygonPoints.getManchesterPoints(mapViewer);
    Polygon polygon = mapViewer.createPolygon(coordinates);
    mapViewer.addPolygonOverlay(polygon);
    polygonNames.put(((PolygonDelegate) polygon).getPolygon().getId(), "manchester");

    mapViewer.addPolygonClickListener(event -> {
        Polygon clickedPolygon = event.getPolygon();
        String polygonName = polygonNames.get(((PolygonDelegate) clickedPolygon).getPolygon().getId());

        if ("manchester".equals(polygonName)) {
            showNotification("Manchester polygon clicked");
        }
    });
}

I attached a sample project: map-test.zip (79.6 KB)

1 Like

Thanks,

It works like a charm. Just curious if we will have this as a direct method in Polygon in future versions. Also same for Markers.