【发布时间】:2017-06-22 12:10:44
【问题描述】:
所以我有一张地图和一个表格,一旦用户填写地址 + 邮政编码,它就会在地图上放置一个标记,但是我想让它可拖动并保持 lat 和 long 的值。
由于某种原因,我无法移动标记,为什么?
JS:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {lat: -34.397, lng: 150.644}
});
var geocoder = new google.maps.Geocoder();
document.getElementById('postcode').addEventListener('blur', function() {
geocodeAddress(geocoder, map);
});
}
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address2').value +','+ document.getElementById('postcode').value;
console.log(address);
geocoder.geocode({'address': address}, function(results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location,
title: address,
draggable:true,
});
google.maps.event.addListener(marker, 'dragend', function(marker){
var latLng = marker.latLng;
currentLatitude = latLng.lat();
currentLongitude = latLng.lng();
jQ("#latitude").val(currentLatitude);
jQ("#longitude").val(currentLongitude);
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
【问题讨论】:
-
我认为我回答了一个非常相似的问题。 stackoverflow.com/questions/44030800/…
标签: javascript jquery google-maps google-maps-api-3