2016-02-19 119 views
0

我正在使用google maps JavaScript API,並且當前我正在嘗試創建LatLngBounds對象,以便我可以將其應用於基於位置的Google地圖在一個數組中。嘗試從有效LatLng對象創建LatLngBounds對象時出錯

我發現西南部和東北部最高分,創造從他們經緯度對象,然後試圖用這些來創建對象LatLangBounds:

centerMap: function(){ 
    var lowestLatitude = this.locations.reduce(function (carry, nextLocation, index){ 
      return carry < nextLocation.position.lat && carry !== null ? carry : nextLocation.position.lat; 
    }, null); 

    var lowestLongitude = this.locations.reduce(function (carry, nextLocation, index){ 
      return carry < nextLocation.position.lng && carry !== null ? carry : nextLocation.position.lng; 
    }, null); 

    var highestLatitude = this.locations.reduce(function (carry, nextLocation, index){ 
      return carry > nextLocation.position.lng && carry !== null ? carry : nextLocation.position.lat; 
    }, null); 

    var highestLongitude = this.locations.reduce(function (carry, nextLocation, index){ 
      return carry > nextLocation.position.lng && carry !== null ? carry : nextLocation.position.lng; 
    }, null); 

    debugger; 
    var southWest = new google.maps.LatLng({ lat: lowestLatitude, lng: lowestLongitude}); 
    var northEast = new google.maps.LatLng({ lat: highestLatitude, lng: highestLongitude}); 
    var bounds = new google.maps.LatLngBounds({sw: southWest, ne: northEast}); 
    this.map.fitBounds(bounds); 
} 

我遇到的錯誤是,當我創建LatLngBounds實例。我得到的錯誤:

Uncaught InvalidValueError: not a LatLng or LatLngLiteral: in property lat: not a number

的事情是,有沒有錯誤創造了西南和東北的經緯度情況下,如此看來奇怪的是,試圖創建兩種有效的經緯度對象範圍將拋出一個錯誤。

gif of the error happening

我缺少什麼導致了此錯誤?

回答

1

看起來你傳遞一個對象的構造函數,而不是2個參數 - 你試過這樣:

var bounds = new google.maps.LatLngBounds(southWest, northEast); 
+1

Aww你對我來說太快了! +1! – Oliver

+0

那麼這是一個facepalm時刻的地獄!感謝您指出了這一點。 –

1

望着documentation的構造函數有兩個參數,而不是一個具有兩個屬性的對象。因此,你需要像下面這樣:

var bounds = new google.maps.LatLngBounds(southWest, northEast); 
1

google.maps.LatLng構造函數不(目前)採取google.maps.LatLngLiteral作爲參數。它需要兩個號碼代表地理座標的經度和緯度(按此順序)。這:

var southWest = new google.maps.LatLng({ lat: lowestLatitude, lng: lowestLongitude}); 
var northEast = new google.maps.LatLng({ lat: highestLatitude, lng: highestLongitude}); 
var bounds = new google.maps.LatLngBounds({sw: southWest, ne: northEast}); 

應該是:

var southWest = new google.maps.LatLng(lowestLatitude, lowestLongitude); 
var northEast = new google.maps.LatLng(highestLatitude, highestLongitude); 
var bounds = new google.maps.LatLngBounds(southWest, northEast); 

proof of concept fiddle

代碼片段:

var geocoder; 
 
var map; 
 

 
function initialize() { 
 
    var map = new google.maps.Map(
 
    document.getElementById("map_canvas"), { 
 
     center: new google.maps.LatLng(37.4419, -122.1419), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    var lowestLatitude = bndsObj.bounds.south; 
 
    var lowestLongitude = bndsObj.bounds.west; 
 
    var highestLatitude = bndsObj.bounds.north; 
 
    var highestLongitude = bndsObj.bounds.east; 
 
    var southWest = new google.maps.LatLng(lowestLatitude, lowestLongitude); 
 
    var northEast = new google.maps.LatLng(highestLatitude, highestLongitude); 
 
    var bounds = new google.maps.LatLngBounds(southWest, northEast); 
 
    map.fitBounds(bounds); 
 

 
} 
 
google.maps.event.addDomListener(window, "load", initialize); 
 
// New York City bounds 
 
var bndsObj = { 
 
    "bounds": { 
 
    "south": 40.4960439, 
 
    "west": -74.2557349, 
 
    "north": 40.91525559999999, 
 
    "east": -73.7002721 
 
    } 
 
};
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map_canvas"></div>