根据视口坐标创建地图

时间:2017-10-10 22:25:40

标签: google-maps google-maps-api-3

创建地图时,是否可以指定视口坐标而不是指定中心坐标?

例如,在给定以下视口坐标的情况下创建地图:

"viewport": {
    "northeast": {
        "lat": 43.36899968029149,
        "lng": -80.08620496970849
    },
    "southwest": {
        "lat": 43.3663017197085,
        "lng": -80.0889029302915
    }
}

目前,我正在使用中心位置:

map = new google.maps.Map(document.getElementById('map'),{
    zoom: 13,
    center: { lat: 48.642312, lng: -80.51857 },
    gestureHandling: 'greedy',
    zoomControl: false,
    mapTypeControl: true,
    scaleControl: false,
    streetViewControl: false,
    fullscreenControl: false
});

1 个答案:

答案 0 :(得分:0)

请参阅the documentation,google.maps.Map对象有一个.fitBounds方法,该方法以google.maps.LatLngBoundsgoogle.maps.LatLngBoundsLiteral为参数。

function initialize() {
  var viewport = {
    north: 43.36899968029149,
    east: -80.08620496970849,
    south: 43.3663017197085,
    west: -80.0889029302915
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"));
  map.fitBounds(viewport);
}

proof of concept fiddle