Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get LatLng from Zip Code - Google Maps API

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.

like image 344
Scott Avatar asked Apr 07 '11 18:04

Scott


People also ask

How do I find lat long from zip code?

(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.

How can I get Google map API zip code?

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.

How do I find LatLng address?

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.


1 Answers

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); 
like image 55
Mark Avatar answered Oct 02 '22 02:10

Mark