[英]Combining Google Maps Geocoder and Leaflet Map Receiving Error: Invalid LatLng object
我正在使用Google Maps Geocoder來獲取地址的經度和緯度。 然后,我要獲取該地址並使用Leaflet,然后將地圖平移至緯度+經度坐標。 但是,我從Firebug收到一條錯誤Error: Invalid LatLng object: (-33.8674869, 151.20699020000006, undefined)
。 我知道我的變量geolocation
返回-33.8674869, 151.20699020000006
但未定義。 是什么原因引起的?
<body onload="initialize()">
<div id="result"></div>
<div>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Geocode" onclick="codeAddress()">
</div>
<div id="map"></div>
<script type="text/javascript">
var map = L.map('map').setView([33.7489954, -84.3879824], 13);
var geocoder;
function initialize() {
geocoder = new google.maps.Geocoder();
}
function codeAddress() {
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var geolocation = String(results[0].geometry.location);
var geolocation = geolocation.replace('(','');
var geolocation = geolocation.replace(')','');
map.panTo([geolocation]);
} else {
$('#result').html('Geocode was not successful for the following reason: ' + status);
}
});
};
L.tileLayer('http://{s}.tile.cloudmade.com/apikeyputhere/997/256/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);
</script>
替換為:
var geolocation = String(results[0].geometry.location);
var geolocation = geolocation.replace('(','');
var geolocation = geolocation.replace(')','');
map.panTo([geolocation]);
接着就,隨即:
map.panTo([results[0].geometry.location.lat(),results[0].geometry.location.lng()]);
說明:
您為第一個數組值分配一個字符串,它與以下內容相同:
geolocation=new Array('-33.8674869, 151.20699020000006')//contains 1 string
該字符串將不被求值,它將保留為1個字符串,但是您需要一個具有2個浮點數的數組:
geolocation=new Array(-33.8674869, 151.20699020000006)//contains 2 floats
undefined
是提供給panTo()
的數組的缺少的第二項
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.