Posts Tagged ‘geocode’

Google Maps V3 GeoCoding Example

July 24, 2011

Today I was working on a real estate search wrapper idea and trying to integrate Google Maps. I wanted to use there Javascript API for mapping but needing to translate the addresses to longitude and latitude aka GeoCoding. Turns out Google of course has an API for this but I didn’t find a straight forward example right away so I figured I would post one. Minor disclaimer on this, its the SIMPLEST possible example to just take one address and turn it into a longitude and latitude (visit here if you need more info on the API). Hopefully this is helpful to folks.

Script Include

Before using the below code you’ll want to add a script include within the head element of your HTML page first.

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

The Code

 

1:  var geocoder = new google.maps.Geocoder();  
2:  var results = geocoder.geocode(  
3:    {  
4:      address: "Duke University Durham, NC 27708"  
5:    },   
6:    function callback(results){  
7:      alert(results[0].geometry.location);  
8:    }  
9:  );