【发布时间】:2016-02-02 12:08:57
【问题描述】:
实际上,我有一张带有可拖动标记的地图。这很好用,但我无法更新拖动标记的 lat 和 lng。我已经尝试了几个不同的事件监听器,但它们都不起作用。现在我想知道是否有人可以帮助我?
我的代码(我删除了你不会混淆的脚本):
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: {
lat: 47.532446,
lng: 14.623283
}
});
var geocoder = new google.maps.Geocoder();
document.getElementById('geolocate').addEventListener('click', function () {
geocodeAddress(geocoder, map);
});
}
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('street').value + ' ' +
document.getElementById('zip').value + ' ' +
document.getElementById('city').value;
geocoder.geocode({
'address': address
}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
resultsMap.setCenter(results[0].geometry.location);
document.getElementById('lat').value = results[0].geometry.location.lat();
document.getElementById('lng').value = results[0].geometry.location.lng();
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location,
draggable: true
});
resultsMap.setZoom(17);
resultsMap.panTo(marker.position);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
如果有人可以帮助我,我会很高兴。
【问题讨论】:
-
请提供一个Minimal, Complete, Tested and Readable example 来说明问题,包括任何必需的 HTML/CSS。
标签: javascript jquery google-maps-api-3 google-maps-markers