在谷歌地图中,当我拖动文本框中显示的标记lat()
和lng()
值时。我想做这里:
我的以下代码部分对我有用。但是,当我将标记拖到远处时,它可以工作,但距离很近 lat()
并且lng()
不会改变?
我该如何解决这个问题?
主要的js代码是:
// fill in the UI elements with new position data
function update_ui(address, lat, lng) {
$('#lat').val(lat);
$('#lng').val(lng);
}
// move the marker to a new position, and center the map on it
function update_map(geometry) {
map.fitBounds(geometry.viewport)
marker.setPosition(geometry.location)
}
和地理编码:
geocoder.geocode(request, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// Google geocoding has succeeded!
if (results[0]) {
// Always update the UI elements with new location data
update_ui(results[0].formatted_address, results[0].geometry.location.lat(), results[0].geometry.location.lng())
// Only update the map (position marker and center map) if requested
if (update) { update_map(results[0].geometry) }
} else {
// Geocoder status ok but no results!?
alert("Error occured. Please try again.");
}
} else {
// Google Geocoding has failed. Two common reasons:
// * Address not recognised (e.g. search for 'zxxzcxczxcx')
// * Location doesn't map to address (e.g. click in middle of Atlantic)
if (type == 'address') {
alert("Sorry! We couldn't find " + value + ". Try a different search term, or click the map.");
} else {
alert("Sorry! We couldn't find " + value + ". Try a different search term, or click the map.");
update_ui('', value.lat(), val.lng())
}
};
});
编辑:
当我很少拖动标记时,坐标不会改变,但经过一些移动它们会改变。这是jsfiddle: