The following is my reverse geocoding javascript:
google.maps.event.addListener(marker, 'drag', function() {
geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$.each(results[0].address_components, function(i,v) {
if (v.types[0] == 'postal_code') {
$('#zip').val(v.long_name);
}
if (v.types[0] == 'locality') {
$('#city').val(v.long_name);
}
if (v.types[0] == 'administrative_area_level_1' || v.types[0] == 'administrative_area_level_2') {
$('#state').val(v.long_name);
}
if (v.types[0] == 'country') {
$('#country').val(v.long_name);
}
});
$('#address').val(results[0].formatted_address);
$('#lat').val(marker.getPosition().lat());
$('#lng').val(marker.getPosition().lng());
} else {
alert("No results found. Please revise your address or adjust your marker.");
}
});
});
First when the result for City is AAA, then the input[type=text]#city will be AAA. When I reverse geocode again, the return address does not have city in it. Instead of clearing the previous values, the AAA is left in the input text field.
Is there any way to clear the field when no results returned?
Thanks.
just clear it when the function is run like so
Do the same for any other fields you need to clear