Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6896037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:59:02+00:00 2026-05-27T06:59:02+00:00

function initialize() { var latlng = new google.maps.LatLng(37.7702429, -122.4245789); var myOptions = { zoom:

  • 0
function initialize() {
    var latlng = new google.maps.LatLng(37.7702429, -122.4245789);

    var myOptions = {
      zoom: 3,
      center: latlng,
      disableDefaultUI: false,
      mapTypeId: google.maps.MapTypeId.TERRAIN,
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

    // Limit panning
    // Latitude bounds for map, longitude not important
    var southWest = new google.maps.LatLng(-85.000, -122.591);
    var northEast = new google.maps.LatLng(85.000, -122.333);
    var allowedBounds = new google.maps.LatLngBounds(southWest, northEast);

    // Add a move listener to restrict the bounds range
    google.maps.event.addListener(map, "center_changed", function() {checkBounds(); });

    //If zoom out at bound limit then map breaks; center doesn't change but bounds get broken. Listen for zoom event and try to correct bound break. **Doesn't Work**
    google.maps.event.addListener(map, 'zoom_changed', function() {checkBounds(); });

    // If the map position is out of range, move it back
    function checkBounds() {

      // Perform the check and return if OK
      if ((allowedBounds.getNorthEast().lat()>(map.getBounds().getNorthEast().lat()))&&(allowedBounds.getSouthWest().lat()<(map.getBounds().getSouthWest().lat()))) {
        lastValidCenter = map.getCenter();
        lastValidZoom = map.getZoom();
        return;
      }

      // not valid anymore => return to last valid position
      map.panTo(lastValidCenter);
      map.setZoom(lastValidZoom);
    }
  }

Basically I don’t want the user to be able to see anything outside of the map, so I have restricted the latitudinal bounds. Works normally.

The issue is that if a user we to be viewing close to the bound limits and then zooms out so that the center doesn’t change, but now the view-port bounds are outside of the bound limit, it does not correct and the map becomes unpannable.

Any help you geniuses can offer is mucho appreciated .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:59:03+00:00Added an answer on May 27, 2026 at 6:59 am

    Ok, so it was very simple. The zoom_changed event was not behaving as expected and bounds_changed on it’s own was not satisfactory. This map will not go out of bounds by pan or zoom and is perfect for if you want the user to only see map and no grey background. Not so good if your users want to center the map at a high latitude and low zoom level. Cross that bridge later. Here’s the code:

        // The allowed region which the whole map must be within
        var southWest = new google.maps.LatLng(-85.000, -122.591);
        var northEast = new google.maps.LatLng(85.000, -122.333);
        var allowedBounds = new google.maps.LatLngBounds(southWest, northEast);
    
        // Add listeners to trigger checkBounds(). bounds_changed deals with zoom changes.
        google.maps.event.addListener(map, "center_changed", function() {checkBounds(); });
        google.maps.event.addListener(map, 'bounds_changed', function() {checkBounds(); });
    
        // If the map bounds are out of range, move it back
        function checkBounds() {
          // Perform the check and return if OK
          if ((allowedBounds.getNorthEast().lat()>(map.getBounds().getNorthEast().lat()))&&(allowedBounds.getSouthWest().lat()<(map.getBounds().getSouthWest().lat()))) {
            lastValidCenter = map.getCenter();
            lastValidZoom = map.getZoom();
            return
          }
          // not valid anymore => return to last valid position
          map.panTo(lastValidCenter);
          map.setZoom(lastValidZoom);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

> <script type=text/javascript src=http://maps.google.com/maps/api/js?sensor=true> </script> <script type=text/javascript> function initialize() { var latlng = new
I have the following situation: var Task = Backbone.Model.extend({ initialize: function() { }, save:
I've been playing around with the google maps api through javascript (I'm also new
I'm testing the new Symbols object of the Google Maps V3 API. I have
I have in my code this var map; function initialize() { var mapDiv =
In the Google Maps API V3, I've created a map object: map = new
im fairly new to the Google Maps V3 API and i'm running into an
this is my demo code : <body onload=initialize()> <script> function initialize(){ var d='adddd' $.getScript('other.js',
var Helloworld = { onLoad: function() { // initialization code this.initialized = true; },
var JavascriptHelper = Backbone.Model.extend(JavascriptHelper, {}, // never initialized as an instance { myFn: function()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.