Make clickable polygons on Google Maps (for Android)

前端 未结 3 436
一整个雨季
一整个雨季 2021-01-02 07:34

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 poly
3条回答
  •  独厮守ぢ
    2021-01-02 08:16

    In adding Polygon to the map. First create a PolygonOptions object and add some points to it. These points will form the outline of the polygon. You then add the polygon to the map by calling GoogleMap.addPolygon(PolygonOptions) which will return a Polygon object. This following code snippet show how to add polygon to a map.

    // Instantiates a new Polygon object and adds points to define a rectangle PolygonOptions rectOptions = new PolygonOptions()
                  .add(new LatLng(37.35, -122.0),
                       new LatLng(37.45, -122.0),
                       new LatLng(37.45, -122.2),
                       new LatLng(37.35, -122.2),
                       new LatLng(37.35, -122.0));
    
    // Get back the mutable Polygon Polygon polygon = myMap.addPolygon(rectOptions);
    

    By default, polygons are not clickable. You can enable and disable the clickability by calling Polygon.setClickable(boolean).

    Like N Dorigatti said. In using OnPolygonClickListener to listen click events, call GoogleMap.setOnPolygonClickListener(OnPolygonClickListener).

    When a user clicks on a polygon, you will receive an onPolygonClick(Polygon) callback. Check this document for more information.

提交回复
热议问题