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.
(We could force the locale – that the postcode is in the SG– by using"http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=POSTCODE,SG".) Simply call this url in a jquery-ajax and you will get the lat long in result.
Just query the geocode API with the following string "zip XXXXX" and if you have a valid zip, they will send you back the lat/lon and more.
There is a last trick to get Address from Lat-Long (Geo-coordinates). You can simply hit google-maps web service passing the Latitude and longitude. It is simply a GET-Method web-service.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With