Google geojsonlayer полигонов с эффектами наведения с помощью Android - PullRequest
0 голосов
/ 10 сентября 2018

Я запутался в том, чтобы создавать эффекты наведения для многоугольников в слое геоджонов с помощью android google map. Я восстановил детали геоджона с веб-сервера, чтобы показать вещи на карте. в слое geojson, когда пользователь перемещается по нему. Ниже я разместил свой код, как я реализовал слой geojson, используя карту Google. Заранее спасибо.

сильный текст Класс 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 голосов
/ 13 сентября 2018

Наконец я сделал это с кодом ниже.

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");


        }

    }

}

Одна проблема - я не могу получить свойства нескольких полигонов, пока мы щелкаем по ней. Любая помощь будет полезна.

...