英文:
How to show the places or Area boundaries in google map Android?
问题
我在官方地图文档中搜索了几个小时,但找不到如何在 Android Google 地图中添加城市边界的线索。在 JavaScript 中的 Web 版本上是可用的:https://developers.google.com/maps/documentation/javascript/dds-boundaries/style-polygon#maps_boundaries_simple-javascript,但在 Android 上却不可用。在 Android 上有没有某种可能性?
我尝试了在地图中样式化边界多边形,使用数据驱动的样式,但没有找到有用的信息。
英文:
I have searched for hours in the official maps docs and there was no clue how to add city boundaries in android google map. Its available for web in JavaScript https://developers.google.com/maps/documentation/javascript/dds-boundaries/style-polygon#maps_boundaries_simple-javascript. but for android its not available. Is this possible in android some way?
I tried to Style a boundary polygon, data-driven style in map but didn't find anything useful.
答案1
得分: 1
使用GeoJSON文件显示区域边界
如评论中所述,使用GeoJSON文件可以实现这一目的,您可以通过使用Google地图Android GeoJSON实用工具来实现。
根据文档:
> GeoJSON是JSON数据格式的扩展,表示地理数据。使用此实用程序,您可以将地理要素存储为GeoJSON格式,并将其呈现为地图的图层。要将您的GeoJSON数据添加到地图上并从地图中移除它,请分别调用addLayerToMap()
和removeLayerFromMap()
。同样,您可以通过调用addFeature()
和removeFeature()
并传入GeoJsonFeature对象来添加和移除单个要素。如果您想要访问要素,可以调用getFeatures()
以获取已添加到图层中的所有GeoJsonFeature对象的可迭代对象。
例如,我们可以使用一个包含美国边界的多边形的GeoJSON文件,命名为usa.json
,并将其保存到您的resource/raw
文件夹中。
usa.json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"stroke": "#f53b3b",
"stroke-width": 2,
"stroke-opacity": 1,
"fill": "#555555",
"fill-opacity": 0.5
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[-124.45312499999999, 48.22467264956519],
...
[-124.45312499999999, 48.22467264956519]
]
],
...
]
},
"properties": {
"title": "MultiPolygon United States of America"
}
}
]
}
然后,您可以创建一个函数来从资源文件夹中检索文件,如下所示:
private void retrieveFileFromResource() {
try {
GeoJsonLayer layer = new GeoJsonLayer(getMap(), R.raw.usa, this);
// 这将是将图层添加到地图上的函数,我也会将其放在下方。
addGeoJsonLayerToMap(layer);
} catch (IOException e) {
Log.e(mLogTag, "无法读取GeoJSON文件");
} catch (JSONException e) {
Log.e(mLogTag, "无法将GeoJSON文件转换为JSONObject");
}
}
最后,将图层添加到地图上:
private void addGeoJsonLayerToMap(GeoJsonLayer layer) {
layer.addLayerToMap();
// 通过GeoJsonLayer点击演示接收要素。
layer.setOnFeatureClickListener(new GeoJsonLayer.GeoJsonOnFeatureClickListener() {
@Override
public void onFeatureClick(Feature feature) {
Toast.makeText(GeoJsonDemoActivity.this,
"单击的要素:" + feature.getProperty("title"),
Toast.LENGTH_SHORT).show();
}
});
}
它应该看起来类似于这样:
您还可以克隆android-maps-utils库并尝试使用其GeoJSON示例。
希望对您有所帮助!
1: https://i.stack.imgur.com/DzaNd.png
英文:
Using a GeoJSON file to show area boundaries
As mentioned in the comment, using a GeoJSON file will make this possible, and you can do so by using the Google Maps Android GeoJSON Utility.
as per the documentation:
> GeoJSON is an extension of the JSON data format and represents
> geographical data. Using this utility, you can store geographical
> features in GeoJSON format and render them as a layer on top of the
> map. To add and remove your GeoJSON data to and from the map, call
> addLayerToMap()
and removeLayerFromMap()
respectively. Similarly you
> can add and remove individual features by calling addFeature()
and
> removeFeature()
and passing in a GeoJsonFeature object. If you want to
> access the features, you can call getFeatures()
to get an iterable of
> all GeoJsonFeature objects that have been added to the layer.
For example, we can use a GeoJSON file that contains a multi polygon of the boundary of USA called usa.json
and save it to your resource/raw
folder.
usa.json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"stroke": "#f53b3b",
"stroke-width": 2,
"stroke-opacity": 1,
"fill": "#555555",
"fill-opacity": 0.5
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-124.45312499999999,
48.22467264956519
],
[
-123.57421875,
39.436192999314095
],
[
-120.32226562500001,
34.45221847282654
],
[
-116.89453125,
32.54681317351514
],
[
-114.873046875,
32.69486597787505
],
[
-110.56640625,
31.27855085894653
],
[
-108.720703125,
31.50362930577303
],
[
-106.5234375,
31.653381399664
],
[
-104.853515625,
30.221101852485987
],
[
-103.095703125,
29.152161283318915
],
[
-102.65625,
29.6880527498568
],
[
-101.689453125,
29.76437737516313
],
[
-97.294921875,
25.799891182088334
],
[
-96.94335937499999,
28.304380682962783
],
[
-93.779296875,
29.458731185355344
],
[
-89.736328125,
29.305561325527698
],
[
-88.76953125,
30.372875188118016
],
[
-83.84765625,
29.916852233070173
],
[
-81.298828125,
25.3241665257384
],
[
-80.068359375,
26.43122806450644
],
[
-82.001953125,
30.977609093348686
],
[
-75.498046875,
35.817813158696616
],
[
-73.564453125,
40.84706035607122
],
[
-66.97265625,
44.5278427984555
],
[
-68.5546875,
47.21956811231547
],
[
-79.189453125,
43.13306116240612
],
[
-83.3203125,
41.83682786072714
],
[
-82.353515625,
44.902577996288876
],
[
-88.24218749999999,
47.81315451752768
],
[
-95.09765625,
48.922499263758255
],
[
-124.45312499999999,
48.22467264956519
]
]
],
[
[
[
-141.064453125,
69.62651016802958
],
[
-152.9296875,
70.64176873584621
],
[
-157.32421875,
70.90226826757711
],
[
-166.376953125,
68.43151284537514
],
[
-160.6640625,
66.30220547599842
],
[
-164.53125,
66.40795547978848
],
[
-168.22265625,
65.62202261510642
],
[
-165.322265625,
64.35893097894458
],
[
-161.19140625,
64.66151739623564
],
[
-161.19140625,
63.35212928507874
],
[
-164.53125,
63.11463763252091
],
[
-166.11328125,
61.60639637138628
],
[
-164.53125,
60.71619779357714
],
[
-167.080078125,
60.108670463036
],
[
-162.24609375,
59.7563950493563
],
[
-161.806640625,
58.63121664342478
],
[
-158.115234375,
58.6769376725869
],
[
-168.3984375,
52.908902047770255
],
[
-157.1484375,
56.992882804633986
],
[
-153.80859375,
56.70450561416937
],
[
-151.962890625,
57.938183012205315
],
[
-148.7109375,
60.19615576604439
],
[
-145.810546875,
60.326947742998414
],
[
-140.9765625,
60.1524422143808
],
[
-141.064453125,
69.62651016802958
]
]
],
[
[
[
-160.20263671875,
21.80030805097259
],
[
-159.63134765625,
22.248428704383624
],
[
-159.30175781249997,
22.14670778001263
],
[
-156.005859375,
20.715015145512087
],
[
-154.75341796875,
19.518375478601566
],
[
-155.76416015625,
18.93746442964186
],
[
-156.02783203124997,
19.766703551716976
],
[
-155.76416015625,
20.076570104545173
],
[
-156.4892578125,
20.591652120829167
],
[
-156.99462890624997,
20.756113874762082
],
[
-158.115234375,
21.37124437061831
],
[
-159.45556640625,
21.820707853875017
],
[
-160.20263671875,
21.80030805097259
]
]
]
]
},
"properties": {
"title": "MultiPolygon United States of America"
}
}
]
}
Then you can create a function to retrieve the file from your resource folder like so:
private void retrieveFileFromResource() {
try {
GeoJsonLayer layer = new GeoJsonLayer(getMap(), R.raw.usa, this);
// this will be the function to add the layer on the map and
// I'll put it also below.
addGeoJsonLayerToMap(layer);
} 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");
}
}
Then finally, add the layer on the map:
private void addGeoJsonLayerToMap(GeoJsonLayer layer) {
layer.addLayerToMap();
// Demonstrate receiving features via GeoJsonLayer clicks.
layer.setOnFeatureClickListener(new GeoJsonLayer.GeoJsonOnFeatureClickListener() {
@Override
public void onFeatureClick(Feature feature) {
Toast.makeText(GeoJsonDemoActivity.this,
"Feature clicked: " + feature.getProperty("title"),
Toast.LENGTH_SHORT).show();
}
});
}
It should look something like this:
You can also clone the android-maps-utils library and play around with their GeoJSON samples.
I hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论