2016-02-19 85 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>