Book Image

Google Maps JavaScript API Cookbook

Book Image

Google Maps JavaScript API Cookbook

Overview of this book

Day by day, the use of location data is becoming more and more popular, and Google is one of the main game changers in this area. The Google Maps JavaScript API is one of the most functional and robust mapping APIs used among Geo developers. With Google Maps, you can build location-based apps, maps for mobile apps, visualize geospatial data, and customize your own maps.Google Maps JavaScript API Cookbook is a practical, hands-on guide that provides you with a number of clear, step-by-step recipes that will help you to unleash the capabilities of the Google Maps JavaScript API in conjunction with open source or commercial GIS servers and services through a number of practical examples of real world scenarios. This book begins by covering the essentials of including simple maps for Web and mobile, adding vector and raster layers, styling your own base maps, creating your own controls and responding to events, and including your own events.You will learn how to integrate open source or commercial GIS servers and services including ArcGIS Server, GeoServer, CartoDB, Fusion Tables, and Google Maps Engine with the Google Maps JavaScript API. You will also extend the Google Maps JavaScript API to push its capabilities to the limit with additional libraries and services including geometry, AdSense, geocoding, directions, and StreetView.This book covers everything you need to know about creating a web map or GIS applications using the Google Maps JavaScript API on multiple platforms.
Table of Contents (15 chapters)
Google Maps JavaScript API Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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 there are some additional code blocks to listen for map events and to check for the restricted extent.

You can find the source code at Chapter 5/ch05_restrict_extent.html.

How to do it…

Restricting the map extent is quite easy if you perform the following...