www.webdeveloper.com
Results 1 to 2 of 2

Thread: Need help with Google API LatLng values?

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    95

    Need help with Google API LatLng values?

    I have an array where I store location addresses and the array contains the following values:

    217 Pacific Avenue,Toronto,M6P 2P7,Ontario
    20 Eglinton Avenue West Suite 2100, Box 2014,Toronto,M4R 1K8,Ontario
    45 Sunrise Ave Apt # 415,Toronto,M4A2S3,Ontario

    I am trying to get LatLng values for each value but I am always getting (43.6594037, -79.4659595) value for all values in the array.

    Code:
    for(var i = 0; i < AddressArray.length; i++)
    {
    	geocoder = new google.maps.Geocoder();							
    							
    	if(geocoder)
    	{						
    		geocoder.geocode({'address' : AddressArray[i]}, function(results, status) {
    			if (status == google.maps.GeocoderStatus.OK) 
    			{
    				alert(results[0].geometry.location);
    			} else 
    			{
    				alert("Geocode was not successful for the following reason: " + status);
    			}			
    		});
    	}
    }

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    You have probably to wait for the response for each address with something like this :
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>  
        <head>    
            <meta http-equiv="content-type" content="text/html; charset=utf-8">   
            <title>Geocoding service</title>
    <style type="text/css">
    
    </style>            
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>    
    </head>  
        <body>    
            <div id="pge">
            <div id="rsp"></div>      
            </div>  
    <script type="text/javascript">
    var arrAdd=["217 Pacific Avenue,Toronto,M6P 2P7,Ontario","20 Eglinton Avenue West Suite 2100, Box 2014,Toronto,M4R 1K8,Ontario","45 Sunrise Ave Apt # 415,Toronto,M4A2S3,Ontario"];
    var nos=0; // A global variable
    var geocoder = new google.maps.Geocoder();
    function cdeAdd() {
        geocoder.geocode( {'address': arrAdd[nos]}, 
            function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) { 
                    document.getElementById('rsp').innerHTML+='<p>'+JSON.stringify(results)+'</p>';
                    if (++nos<arrAdd.length) cdeAdd();// A new call, if any, after the response
                } 
                else {alert('Geocode was not successful for the following reason: ' + status);}  
            });
    }
    cdeAdd();
    </script> 
        </body>
    </html>
    I hope you will find latitude and longitude in the many fields of the response...

    EDIT : Its possible to provide :
    Code:
    	var str=JSON.stringify(results,'','<br>').replace(/(?:<br>)+/g,function(a){return '<br>'+(new Array(a.length>>2).join('&nbsp;&nbsp;&nbsp;'))}); 
    	document.getElementById('rsp').innerHTML+='<p>'+str+'</p>';
    Last edited by 007Julien; 08-13-2013 at 09:46 AM. Reason: complement

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles