Sample Image
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.
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()andremoveLayerFromMap()respectively. Similarly you can add and remove individual features by callingaddFeature()andremoveFeature()and passing in a GeoJsonFeature object. If you want to access the features, you can callgetFeatures()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.
{
"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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With