Google Maps Android — Restrict Markers within City limit

Hello all…

In this tutorial we will see how to restrict users to place location markers within the described limit in Google maps.

First let start by creating an Android Project

Android Studio > File > New > New Project > Select Google Maps Activity > Now give project name and package name > Click Finish.

It would take some time to build. Once the project finish building. Open google_maps_api.xml. In XML file you will see an URL pointing towards Google Cloud Console. Open the URL in browser and sign in with your Gmail account. Create an project with the package name and SHA1 key in cloud console. Mostly the URL will have those things in query parameter. So you no need to do it manually. That’s it. You have created the project. You are all set to rock!! 😎 😎

Run the project. You will see an screen with google map like the below image.

Now all you have to do is to add OnMapClickListener to the Google Map variable . In our case it is mMap.

override fun onMapReady(googleMap: GoogleMap) {
// initialize google map
mMap = googleMap
// Set on click listener for Map
mMap.setOnMapClickListener {
// Add marker to map. Once user click the map.
mMap.addMarker(it?.let { MarkerOptions().position(it) })
}
}

setOnMapClickListener function will give latlng of the clicked position in Google map. This method will be called every time once user click the map. Based on this position we can add marker to our map.

At this point you will able to plot marker all around the google map.

To restrict marker within a particular limit in Google maps. We need to create polygon. Which can be done adding polygon options to Google map

// Add polygons to indicate areas on the map.

val latLngList = arrayListOf(LatLng(-27.457, 153.040),
LatLng(-33.852, 151.211),
LatLng(-37.813, 144.962),
LatLng(-34.928, 138.599))
googleMap.addPolygon(PolygonOptions()
.clickable(false)
.addAll(latLngList)
.fillColor(getColor(R.color.polygonColor))
.strokeWidth(0f))

Most important thing to consider is to set clickable as false for polygonOptions. We can pass the required Latitude and longitude to form an polygon. Build and run the project. You will see the below screen.

It is time to restrict markers to be placed within the polygon limit.

Add the following dependency to app.gradle file

implementation 'com.google.maps.android:android-maps-utils:2.0.3'

Now check the following condition inside setOnMapClickListener

if (PolyUtil.containsLocation(it, latLngList, false)) {
mMap.addMarker(it?.let { MarkerOptions().position(it) })
}
else {
// Marker placed outside the limit of polygon. Throw error.
}

As we can see here, we need to pass the latLng of the user clicked position in google map , Latlng list of the location range and the last parameter is geodesmic. The value is based if The polygon is formed of great circle segments if geodesic is true, and of rhumb (loxodromic) segments otherwise.
Polygon is created only for graphical representation. Now the markers can be placed only inside the limit.

How to restrict this within city or country limit?
It is simple, fetch latitude and longitude value of city from the below api
https://nominatim.openstreetmap.org/search.php?q=salem,tamilnadu&polygon_geojson=1&format=json
Based on the data available in key geojson response you can use it to create an polygon and add our validations mentioned above. The data available in OSM may not be accurate. We may have to do slight modifications.

Bonus

The API also give latlong bounds in response. This can be used to restrict Google map within a particular limit. It basically prevents the user from scrolling or panning in the map to other areas. The camera function of google maps is restricted to the bound area. This can be achieved by setting latlng bound to google map.

Thanks for reading…

Google Certified Android Developer