Saturday, June 1, 2024
186
rated 0 times [  187] [ 1]  / answers: 1 / hits: 177837  / 13 Years ago, thu, april 7, 2011, 12:00:00

All I want is some simple example code that shows me how to obtain a latlng element from an inputted zip code OR a city/state.


More From » google-maps-api-3

 Answers
2

Couldn't you just call the following replaceing the {zipcode} with the zip code or city and state
http://maps.googleapis.com/maps/api/geocode/json?address={zipcode}



Google Geocoding



Here is a link with a How To Geocode using JavaScript: Geocode walk-thru. If you need the specific lat/lng numbers call geometry.location.lat() or geometry.location.lng() (API for google.maps.LatLng class)



EXAMPLE to get lat/lng:



    var lat = '';
var lng = '';
var address = {zipcode} or {city and state};
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
lat = results[0].geometry.location.lat();
lng = results[0].geometry.location.lng();
});
} else {
alert(Geocode was not successful for the following reason: + status);
}
});
alert('Latitude: ' + lat + ' Logitude: ' + lng);

[#92860] Wednesday, April 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mari

Total Points: 305
Total Questions: 100
Total Answers: 98

Location: Somalia
Member since Mon, Feb 27, 2023
1 Year ago
;