谷歌geojsonlayer多边形与悬停效果使用Android
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谷歌geojsonlayer多边形与悬停效果使用Android相关的知识,希望对你有一定的参考价值。
我很困惑使用android谷歌地图为geojson层中的多边形做悬停效果。我已经从web服务器恢复geojson细节以显示map.i上的东西。我想在geojson中为多边形做悬停效果用户手指在它上面移动时的图层。我发布了我的代码,我是如何使用谷歌地图实现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");
}
}
}
答案
最后我用下面的代码制作了它。
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");
}
}
}
一个问题是,当我们点击它时,我无法获得多个多边形的属性。任何帮助都将是值得欣赏的。
以上是关于谷歌geojsonlayer多边形与悬停效果使用Android的主要内容,如果未能解决你的问题,请参考以下文章