Guru Guru - 5 months ago 34
Android Question

How to make map object (eg: circle) clickable in android google maps v2?

Google recently introduced the circle as a map object in V2 and can easily be drawn:
https://developers.google.com/maps/documentation/android/shapes#circles

Does anyone know how to make this clickable, so that I can open an info window etc... ?

Thanks a lot for the help.

Answer

For Circles it should be easy: use OnMapClickListener and this code:

void onMapClick(LatLng position) {

    LatLng center = circle.getCenter();
    double radius = circle.getRadius();
    float[] distance = new float[1];
    Location.distanceBetween(position.latitude, position.longitude, center.latitude, center.longitude, distance);
    boolean clicked = distance[0] < radius;

}

You can of course iterate over circles if you have more of them.

For others you have to code checking yourself. The algorithm for Polygon is not hard to find online.