问题
In google map when I drag a marker lat()
and lng()
values shown in textboxes. I want do to like here:
Coordinates change for every movement
My following code parts work for me. But, when I drag a marker to far, it works, but in
little distances lat()
and lng()
don't change?
How can I solve this issue?
main js codes are:
// 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)
}
And geocode:
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())
}
};
});
Edit:
When I drag a marker very little, coordinates do not change, but after some movement they change. Here is jsfiddle:
http://jsfiddle.net/jhoonbey/wwdE9/
回答1:
The problem is that you're passing the marker's position through a geocoder.
A geocoder takes a raw lat/lng location and tries to map it to an address. This could be a street or a town or a region. The results that the Google geocoder return include the address text, plus the location of that address.
You're displaying the location that the geocoder returns. As the user moves the marker around regions with a low density of potential addresses, you're going to see aliasing. That means that the various raw locations that the user 'selects' will reverse geocode down to the same address being returned by the geocoder.
Thus, it will appear as if the lat/lng of the marker is not changing, when what you're actually doing is displaying the lat/lng of the geocoded address.
Note, that site that you linked to doesn't do any geocoding when it displays position information.
来源:https://stackoverflow.com/questions/17587352/geocode-does-not-determine-point-location-exactly-on-google-map