User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 
When you want a geocode an address and your not using a bulk method (real time entry). Here is a great little snippet of code to get you started.
Google does have a limit of 2000 address a day.
You could also hide the code to do geocoding of items that were missed by using a jquery or ajax:
  • when the page load put an address in a var and the id of the address in another var.
  • in the call back have it jquery a url with the new lat,lng, and address id
  • on your call backpage just update the lat,lng where the id = what you passed


  • <html>
    <head>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script>
    geocoder = new google.maps.Geocoder();
    function DoAddress()
    {
    	var InAddress = document.getElementById('address').value;
    	var geocoder = new google.maps.Geocoder();  
    	var results = geocoder.geocode
    	(  
    		{  
    			address: InAddress  
    		},   
    			function callback(results)
    			{  
    				document.getElementById('lat').value= results[0].geometry.location.lat();
    				document.getElementById('lng').value= results[0].geometry.location.lng();
    			}  
    	);  
    }
    </script>
    </head>
    <body>
    <br />
    <input type="text" style="width:300px;" name="address" id="address" value="austin tx 78747" /> 
    <input type="button" value="GeoCode" onclick="DoAddress()" /><br /><br />
    Lat: <input type="text" style="width:200px;" name="lat" id="lat" value="" /><br />
    Lng: <input type="text" style="width:200px;" name="lng" id="lng" value="" /><br />
    
    
    </body>
    </html>
    



    This is my version of hidding the geocoded address for bulk but on client side
    This is PHP and Jquery.
    Why did I do this? I have 90k address that need to be geocoded. I generally have 50 people working/using the site.
    geocoder = new google.maps.Geocoder();
    function DoAddress()
    {
    	var InAddress = '';
    	var InId = '';
    	var geocoder = new google.maps.Geocoder();  
    	var results = geocoder.geocode
    	(  
    		{  
    			address: InAddress  
    		},   
    			function callback(results)
    			{  
    				if(typeof(results[0]) != "undefined")
    				{
    					var mylat= results[0].geometry.location.lat();
    					var mylng = results[0].geometry.location.lng();
    					
    					var pullurl = "/somefolder/updategeocode?i=" + InId + "&lng=" + mylng + "&lat=" + mylat + "&rn=" + Math.floor(Math.random() * 100000); 
    					//rn is just to keep browser fresh
    
    					try {
    						if (pullurl) {
    							jQuery.get(pullurl, {},
    							function (data) {
    								//data is your pages reponse.
    								//we really don't care about it.
    								//if valid geocode we have just updated the address
    								//since our address our being pulled based on no lat,lng
    							}
    							, 'html');
    						}
    					}
    					catch (e) { }
    				}//end undefined
    			}//end call back  
    	);  
    }
     $(function (){
    	DoAddress();//Call this after page is loaded
      });