2016-02-21 121 views
1

我創建了谷歌地圖API(JavaScript)的地圖如下:谷歌地圖API的Javascript - bounds.contains方法

var currentLocation = {lat: 47.3663615, lng: 8.5388539}; 
var map = new google.maps.Map(document.getElementById('map'), { 
    zoom: zoomLevel, 
    center: currentLocation, 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
}); 

然後我試圖使用bound.contain函數來檢查是否不同經緯度位置被包含在地圖範圍或不在:

var center = map.getCenter(); // works fine 
var bounds = map.getBounds(); // works fine 
var x = bounds.contains(center); // works fine, delivers true 
var a = bounds.contains(currentLocation); throws an error (Uncaught TypeError: a.lat is not a function) 

一些調試後,似乎currentLocation對象不具備與方法處理正確的格式「載」。請注意,我對JavaScript和Google Maps API相當陌生。

在此先感謝您的幫助。

回答

4

LatLngBounds.contains當前需要一個LatLng作爲參數,LatLngBoundsLiteral將不被接受。

使用

var currentLocation = new google.maps.LatLng(47.3663615,8.5388539); 
+0

修正的建議。現在工作得很好。非常感謝您的及時答覆和提示。 – pete