Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use zip code instead Lat and Lng in Google maps API

Tags:

I wonder if there is a way to use zipcode instead of Lat and Lng, using this snippet or using Geocoding Requests.

    <script type="text/javascript">         function initialize() {             var latlng = new google.maps.LatLng(22.1482635,-100.9100755);             // var latlang = new google.maps.zipcode(7845); <- Is there a way to do that?             var myOptions = {                 zoom: 8,                 center: latlng,                 mapTypeId: google.maps.MapTypeId.ROADMAP             };              var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);             var marker = new google.maps.Marker({                 position: latlng,                 map: map,                 title:"zipcode"             });          }     </script> 
like image 707
Ivan Rivera Avatar asked Jun 21 '12 22:06

Ivan Rivera


People also ask

How can I get Google Map API zip code?

ZIP code lookup The request format is: http://maps.googleapis.com/maps/api/geocode/json?address=ZIP_CODE&key=YOUR_API_KEY will, and in the response you get a lot of information, including for our purposes, that 92101 is the postal code for San Diego, California.

How do I find the latitude and longitude of a zip code using Google API?

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


1 Answers

You can use the Google GeoCode API to request the latitude and longitude for the zipcode and go from there.

The API Request URL would look like this if you wanted to do this directly or via some other medium

http://maps.googleapis.com/maps/api/geocode/json?address=50323&sensor=false

Where 50323 is your zip code.

Or you could use the google Javascript API to do this all via jQuery.

var geocoder; //To use later var map; //Your map function initialize() {   geocoder = new google.maps.Geocoder();   //Default setup   var latlng = new google.maps.LatLng(-34.397, 150.644);   var myOptions = {     zoom: 8,     center: latlng,     mapTypeId: google.maps.MapTypeId.ROADMAP   }   map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); }  //Call this wherever needed to actually handle the display function codeAddress(zipCode) {     geocoder.geocode( { 'address': zipCode}, function(results, status) {       if (status == google.maps.GeocoderStatus.OK) {         //Got result, center the map and put it out there         map.setCenter(results[0].geometry.location);         var marker = new google.maps.Marker({             map: map,             position: results[0].geometry.location         });       } else {         alert("Geocode was not successful for the following reason: " + status);       }     });   } 
like image 188
Mitchel Sellers Avatar answered Sep 22 '22 12:09

Mitchel Sellers