点击Google地图中的PolyGon事件

时间:2016-12-12 07:56:22

标签: android google-maps polygons

我正在尝试创建一个可点击的PolyGon.Even我尝试了已经在此StackOverFlow中回答的所有方法。但没有任何工作。 我的代码如下,

JSONObject jsonObj = new JSONObject(result);
                    final JSONArray jsonArray = jsonObj.getJSONArray("zones");
                    int i;

                    System.out.println("PolyGon---> Response   jsonArray " + jsonArray);
                    for (i = 0; i < jsonArray.length(); i++) {
                        JSONArray jsonArrayPoly = jsonArray.getJSONObject(i).getJSONArray("coordinates");
                        System.out.println("PolyGon---> Response   coordinates " + jsonArrayPoly);
                        polygonOptions = new PolygonOptions();
                        polygonOptions.strokeColor(Color.BLACK);
                        polygonOptions.strokeWidth(2);
                        polygonOptions.fillColor(getResources().getColor(R.color.zone));
                        for (int j = 0; j < jsonArrayPoly.length(); j++) {
                            JSONObject jsonCoordinate = jsonArrayPoly.getJSONObject(j);
                            System.out.println("PolyGon---> Response   lat " + jsonCoordinate.getDouble("lat") + " ----> Long");
                            polygonOptions.add(new LatLng(jsonCoordinate.getDouble("lat"), jsonCoordinate.getDouble("lng")));
                        }

                        googleMap.addPolygon(polygonOptions);

                    }

                    polygonOptions.clickable(true);

                    googleMap.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
                        public void onPolygonClick(Polygon polygon) {
                            Toast.makeText(getApplicationContext(), "Problem reading list of markers.", Toast.LENGTH_LONG).show();

                        }
                    });

您的答案将更受欢迎。

1 个答案:

答案 0 :(得分:2)

在将多边形添加到地图之前设置PolygonOptions.clickable

polygonOptions.clickable(true);
googleMap.addPolygon(polygonOptions);