Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make clickable polygons on Google Maps (for Android)

I have continuous LatLngs of various areas in a city. Is there any way I can create clickable polygons with it. Once way to go about would be to

  • Generate polygons with the available LatLngs.( I want to visually show the polygons on the map with color encoding)
  • Set up setOnMapClickListener.
  • Do a point inside polygon test.

I understand that this is very naive. What are the alternative approaches?

like image 934
bittterbotter Avatar asked Mar 14 '16 08:03

bittterbotter


2 Answers

you don't need to go crazy for having clickable polygon. I did it time ago but now, there is an api for that:

GoogleMap.setOnPolygonClickListener(OnPolygonClickListener)

You can use it easily:

GoogleMap mymap =....//init your map
mymap.setOnPolygonClickListener(new OnPolygonClickListener(){
 void onPolygonClick(Polygon polygon){
   //do whatever with polygon!
 }
});
like image 125
N Dorigatti Avatar answered Nov 11 '22 08:11

N Dorigatti


Here's how I did it.

    Polygon polygon = getMap().addPolygon(new PolygonOptions()
                    .add(new LatLng(12.780712, 77.770956), new LatLng(12.912006, 77.229738), new LatLng(12.412006, 77.629738), new LatLng(12.912006, 77.229738))
                    .strokeColor(0xFF00AA00)
                    .fillColor(0x2200FFFF)
                    .strokeWidth(2)
    );


    polygon.setClickable(true);

    getMap().setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
        public void onPolygonClick(Polygon polygon) {

            mClusterManager = new ClusterManager<MyItem>(getApplicationContext(), getMap());
            getMap().setOnCameraChangeListener(mClusterManager);
            getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(getMap().getCameraPosition().target, getMap().getCameraPosition().zoom));

            try {
                readItems();
            } catch (JSONException e) {
                Toast.makeText(getApplicationContext(), "Problem reading list of markers.", Toast.LENGTH_LONG).show();
            }

        }
    });

Hope that helps.

like image 12
bittterbotter Avatar answered Nov 11 '22 09:11

bittterbotter