Google Maps API 3 - limit pan/map bounds

前端 未结 3 1703

I am completely new to Google Maps and am just creating my first map so that I can incorporate it into my website.

I am trying to limit the area that the user can m

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 05:21

    Just for anyone who stumbles upon the now-outdated info on this page as I did, the maps API now provides a built-in way to restrict the map viewport bounds via the restriction property of the MapOptions interface, see docs here. This example restricts north-south panning from showing Antarctica:

    function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: 20, lng: -52},
          zoom: 3,        
          restriction: {latLngBounds:{north: 83.8, south: -57, west: -180, east: 180}}
        }); 
    }
    

提交回复
热议问题