When working with Google Maps, you might want to restrict the maps scrollable area to a specific region
or set boundaries beyond which users can't pan.
So in this video, we're going to learn about limiting map bounds in Google Maps.
The common use cases for limiting map bounds include restricting the map to a specific city or region,
preventing users from scrolling away from relevant areas, and creating a focused map experience for
specific locations.
You can start implementing the restrictions using lat long bounds and camera updates, and using the
lat long bounds from Camera target.
Also, you can use another advanced boundary enforcement with smooth correction.
Okay, let me start with the basic approach.
Basic restriction of the maps using the lat long bounds and camera updates.
Okay.
so as in the previous videos, we created a map overlay.
It's called Manhattan Bounce.
So if I want to restrict our map to the Manhattan bounds, which is this is the southwest corner and
this is the northeast corner of Manhattan.
So I want to allow the user only to display the Manhattan bounds and don't get outside this bounds.
So I want to restrict the Google map to this bound and not allowing the user to navigate outside this
bound.
I'll copy this.
I'll cut it from here and outside the Google map.
I'll paste it in order to use it inside the Google map parameters, I'll use a new parameter called
properties.
Properties is a parameter in Google Maps composable that allows us to specify map properties.
And if we click on the map properties, we can access a public class.
Having those parameters is building enabled.
Is indoor enabled.
My location enabled is traffic enabled.
Lat long bounce for camera, target map style options, map type, maximum zoom preference and minimum
zoom preference.
And we talked about this map properties when we specified the map type and the zoom levels.
Now we're going to use the lat long bounds for camera target properties equals to map property and set
here lat long bounds for camera target.
Set the Manhattan bounds run again.
And here we have an error.
Let me check.
An argument is already passed for this parameter.
Oops.
We specify this in the previous videos the map type.
So I can remove it because the map type is the default.
Here, let me run again and check the map properties and the Manhattan bounds if it is set it correctly.
You can notice that it is centered like this as a central of the Manhattan bounds.
Okay, now if we scroll up and down, we can't because it is set it as a bound for a camera target.
We can zoom in.
Okay.
But we can't go outside the Manhattan.
Okay, you can see this is the Manhattan.
We can't scroll outside the Manhattan city.
Okay, you can see the restriction.
We put a restriction here for the Google map.
Okay, so this is the lat long bounds for camera target.
We set it correctly for our Manhattan city okay.
Scroll down.
You can notice that when you scroll and you zoom in the restriction is increased.
So let me check.
Now you can notice that the restriction is still here to this island.
If I scroll down, I can't.
Okay?
And I can't go outside Manhattan.
This is the Manhattan.
This is the bounds, the lat long bounds for camera target.
Also, I can specify the map type in order to check it as we did before.
Let me run again.
Notice that I can't navigate outside America if I scroll down and zoom in, get the New York City.
If I scroll zoom in, I can't go outside Manhattan.
Okay, you notice that I didn't move outside Manhattan.
Okay, this is the beauty and this is the power of Google Map customizations.
Moreover, I can specify more parameters of the map properties like minimum zoom preference like ten
F and the maximum zoom preference like 18 F.
Let me run again and see that the minimum zoom preference would be ten F ten times.
Okay, so if you load the map, I can't go outside the minimum zoom and I can zoom in till a specific
level, which is 18.
As we said before, the maximum zoom level is 21.
So I can't go further till 18 or 19 or 20.
I can only set the maximum zoom preference to 18, and the minimum zoom preference is ten f, which
is this.
Okay, so if I click this minus I can't go outside in New York.
Also if I scroll up to the left or right I can't move outside New York City.
Okay.
This is the power of using restriction in Google Maps.
And this is how we set the minimum and maximum zoom level and allow the user to set the specific bound
that users can't go beyond it.