I have an requirement, in which Marker should be placed on clicking the Map within the city limit in Google Maps android.If user clicks on the map outside the city limit. Need to show an warning. I know it is possible to add marker on clicking on the map but how can i restrict it to city limit? I tried exploring the concept of GeoJson to add an layer above my Map.But I am not sure how this would help. Any suggestion appreciated. Thanks
Asked
Active
Viewed 543 times
-2
-
well, you can check lat, long are from which city and then you can compare that respective is same city which you want to restrict else give warning. for reference : https://stackoverflow.com/a/2296416/3989630 – APP Jun 22 '20 at 10:17
-
Is It possible to draw an layer for city using GeoJSON and restrict marker? – Nagendra Hari Karthick Jun 22 '20 at 10:23
-
we can draw polyline on map for that city, for that we need multiple latlong. I will check for can we get city boundary locations . – APP Jun 22 '20 at 10:27
-
can you check this: https://stackoverflow.com/a/31865958/3989630 – APP Jun 22 '20 at 10:29
-
yes but it didn't help – Nagendra Hari Karthick Jun 22 '20 at 10:35
-
1here people are getting polygon for respective city, https://gis.stackexchange.com/a/284305 – APP Jun 22 '20 at 10:38
2 Answers
0
Based on this link I could able to fetch polygons for a particular city. The values are stored to DB and fetched using an API. API returns lat lng boundary of a city, Using this I could able to draw an polygon in shape of City.
val polygon = googleMaps?.addPolygon(PolygonOptions()
.addAll(cityLimit)) // cityLimit LatLng list of City Boundary
polygon?.isClickable = false
polygon?.strokeWidth = 2f
Then onMapClick, I added the following condition
override fun onMapClick(latLng: LatLng?) {
if (!PolyUtil.containsLocation(latLng, cityLimit, false))
// Show error message "Unable to place marker outside city limit"
}
PolyUtil is an Class library of Google Maps sdk. This solved my problem. For more details check this blog

Nagendra Hari Karthick
- 463
- 3
- 15
-1
You should create a Geofence around the area you want the user to select. Then you can evaluate whether it is in the city boundaries.
Have a geofence like 10000m around the city,if the user clicks the map and find that it is not inside the geofence you cancel the request but if it is within the boundaries you create a marker

David Innocent
- 606
- 5
- 16
-
Geofence won't help here. Different cities has different radius. It would be difficult to find out the city radius. I am looking for some in build method from Maps sdk. – Nagendra Hari Karthick Jun 23 '20 at 04:27