-2

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

2 Answers2

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

-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