Restricting the map extent

Google Maps has a worldwide extent that shows almost every street on the earth. You can use the Google Maps JavaScript API for the whole earth, but sometimes you need to show only the related area in the mapping application. You can zoom to a fixed location, but this doesn't stop users from moving to another place that is not in the extent of your application.

In this recipe, we will listen for map events to check if we are in an allowed extent. If we are not in the allowed extent, then we move the map to the allowed center within the extent. We used Turkey's geographic extent in this recipe.

Getting ready

This recipe is still using the same map creation process defined in Chapter 1, Google Maps JavaScript API Basics, but ...

Get Google Maps JavaScript API Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.