【发布时间】:2018-12-09 17:25:16
【问题描述】:
我正在使用传单在地图上显示标记,当我在marker 上click 时,我得到了它的lat 和lng,然后我将这些发送到google maps geocoder 检索地址名称:
var markerCoords = [];
circle.on('click', function (e) {
var curPos = e.target.getLatLng();
markerCoords.push(curPos.lng);
markerCoords.push(curPos.lat);
geocodeLatLng();
});
var geocoder = new google.maps.Geocoder;
function geocodeLatLng(geocoder) {
var latlng = {lat: parseFloat(markerCoords[1]), lng: parseFloat(markerCoords[0])};
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
console.log(results[0].formatted_address);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
}
但它给了我:
Cannot read property 'geocode' of undefined
注意
这条线很好
var latlng = {lat: parseFloat(markerCoords[1]), lng: parseFloat(markerCoords[0])};
就像我做 console.log 一样,我得到了正确的 lat 和 lng
【问题讨论】:
标签: javascript jquery google-maps leaflet google-geocoder