Search code examples
javascriptgoogle-mapsgoogle-maps-api-3google-maps-styling

Setting Boundaries around city in google maps


I'm looking for a way to set boundaries around a specific city/town within the Google maps API V3 using JavaScript

Is this type of thing even supported within the API?

Basically i don't want my users to be able to pan across the map further then the city, including country area's not just city limits.


Solution

  • well the boundry that you want to provide to the city/ town can be done. you just need to draw the polylines or polygons specifying the co ordinates of the boundries of the city.
    visit demos and documentation for overlays in Gmap V3

    try the following code to restrict user to pan across the region.

     // Bounds for North America
       var strictBounds = new google.maps.LatLngBounds(
         new google.maps.LatLng(28.70, -127.50), 
         new google.maps.LatLng(48.85, -55.90)
       );
    
       // Listen for the dragend event
       google.maps.event.addListener(map, 'dragend', function() {
         if (strictBounds.contains(map.getCenter())) return;
    
         // We're out of bounds - Move the map back within the bounds
    
         var c = map.getCenter(),
             x = c.lng(),
             y = c.lat(),
             maxX = strictBounds.getNorthEast().lng(),
             maxY = strictBounds.getNorthEast().lat(),
             minX = strictBounds.getSouthWest().lng(),
             minY = strictBounds.getSouthWest().lat();
    
         if (x < minX) x = minX;
         if (x > maxX) x = maxX;
         if (y < minY) y = minY;
         if (y > maxY) y = maxY;
    
         map.setCenter(new google.maps.LatLng(y, x));
       });
    

    this will restrict user to pan across region of north america