使用Android具有悬停效果的Google geojsonlayer多边形

时间:2018-09-10 11:17:39

标签: android google-maps hover polygon geojson

我很困惑要使用android google map在geojson图层中对多边形进行悬停效果。我已经从网络服务器上恢复了geojson详细信息以显示地图上的内容。我想对多边形进行悬停效果当用户手指在其上移动时,在geojson层中。下面我发布了我如何使用Google Map实现geojson层的代码。

强文本 类DownloadGeoJsonFile扩展了AsyncTask {

    protected void onPreExecute() 
    {
        super.onPreExecute();
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected GeoJsonLayer doInBackground(String... params)
    {
        try
        {
            // Open a stream from the URL
            InputStream stream = new URL(params[0]).openStream();
            String line;
            StringBuilder result = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            while ((line = reader.readLine()) != null)
            {
                // Read and save each line of the stream
                result.append(line);
            }
            // Close the stream
            reader.close();
            stream.close();
            return new GeoJsonLayer(mMap, new JSONObject(result.toString()));
        } catch (IOException e) {
            Log.e(mLogTag, "GeoJSON file could not be read");
        } catch (JSONException e) {
            Log.e(mLogTag, "GeoJSON file could not be converted to a JSONObject");
        }
        return null;
    }

    @Override
    protected void onPostExecute(GeoJsonLayer layer) {
        Log.e("sundar","success");
        progressBar.setVisibility(View.GONE);
        if (layer != null) {
            Log.e("sundar","successsuccess");
            addGeoJsonLayerToMap(layer);
        }
    }
}
private void retrieveFileFromUrl()
{
    new DownloadGeoJsonFile().execute(getString(R.string.geojson_url2));
}
private void addGeoJsonLayerToMap(final GeoJsonLayer layer)
{
    if (layer != null)
    {
        try
        {
            Log.e("sundar", "successsuccess");
            layer.addLayerToMap();
            final GeoJsonPolygonStyle style =layer.getDefaultPolygonStyle();
            style.setStrokeColor(Color.BLACK);
            style.setStrokeWidth(3F);
            mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(20.593683, 78.962883)));

            layer.setOnFeatureClickListener(new GeoJsonLayer.GeoJsonOnFeatureClickListener() {
                @Override
                public void onFeatureClick(Feature feature)
                {
                    Log.e("type",feature.getGeometry().getGeometryType().toString());

                }
            });
        }
        catch(Exception e)
        {
            Log.e("exception","exception occured");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

最后我用下面的代码实现了。

private void addGeoJsonLayerToMap(final GeoJsonLayer layer)

{

    if (layer != null)

    {

        try

        {

            Log.e("sundar", "successsuccess");

            layer.addLayerToMap();

            /*polygon style*/

            final GeoJsonPolygonStyle style = layer.getDefaultPolygonStyle();
            style.setStrokeColor(Color.BLACK);
            style.setFillColor(Color.GRAY);

          /*  style.setFillColor(R.drawable.map_hover_button);*/
            style.setStrokeWidth(3F);

        /*its for setting markers with infowindow*/
            for (GeoJsonFeature feature : layer.getFeatures())
            {
                GeoJsonPointStyle pointStyle=new GeoJsonPointStyle();
                pointStyle.setTitle(feature.getProperty("Name"));
                pointStyle.setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.locpin));
                feature.setPointStyle(pointStyle);
            }
            layer.setOnFeatureClickListener(newGeoJsonLayer.GeoJsonOnFeatureClickListener()

            {
                @Override
                public void onFeatureClick(Feature feature)
                {

            Log.e("type",feature.getGeometry().getGeometryType().toString());
Toast toast=Toast.makeText(getApplicationContext(),feature.getProperty("Name"),Toast.LENGTH_SHORT);

                        toast.show();
                        Handler handler = new Handler();

                        handler.postDelayed(new Runnable()

                        {

                            @Override

                            public void run()

                            {

                                toast.cancel();

                            }

                        }, 500);
                        if(feature.getGeometry().getGeometryType().equalsIgnoreCase("Polygon")||feature.getGeometry().getGeometryType().equalsIgnoreCase("MultiPolygon")) {

                            if (coloredfeature != null)

                            {
                                GeoJsonPolygonStyle lineStringStyle = new GeoJsonPolygonStyle();

                                coloredfeature = (GeoJsonFeature) feature;

                                lineStringStyle.setFillColor(Color.GRAY);

                                lineStringStyle.setStrokeColor(Color.BLACK);

                                lineStringStyle.setStrokeWidth(3f);

                                lineStringFeature.setPolygonStyle(lineStringStyle);

                            }

                            GeoJsonPolygonStyle lineStringStyle = new GeoJsonPolygonStyle();

                            lineStringFeature = (GeoJsonFeature) feature;

                            coloredfeature = (GeoJsonFeature) feature;

                            lineStringStyle.setFillColor(Color.parseColor("#991A6659"));

                            lineStringStyle.setStrokeColor(Color.WHITE);

                            lineStringStyle.setStrokeWidth(2f);

                            lineStringFeature.setPolygonStyle(lineStringStyle);

                        } 

                }

            });

        }

        catch(Exception e)

        {

            Log.e("exception","exception occured");


        }

    }

}

一个问题是,当我们单击它时,我无法获取多个多边形的属性。不胜感激。