Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Is it possible to store the results produced by Geocoder request in a global array? Let's say, I pass an array containing addresses, and I want LatLng results to be stored in a new global array outside the geocode function?
Posted
Comments
DamithSL 6-Nov-14 9:55am    
update question with your code

1 solution

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript">
        var latLong = []; 
        var count = 0;
            
        function getLatLong(){
            $("#btn").unbind('click').click(function(e) {
                e.stopPropagation();
                e.preventDefault();
                
                var geocoder =  new google.maps.Geocoder();
                geocoder.geocode( { 'address': $('#city').val()}, function(results) {
                    latLong[count] = new Array();
                    
                    latLong[count]['lat'] = results[0].geometry.location.lat();
                    latLong[count]['lng'] = results[0].geometry.location.lng();
                    
                    ++count;
                });
            
                console.log(latLong);
            });
        }
         </script>
        <style>
            .push-down {margin-top: 25px;}
        </style>
    </head>
    
    <body onload="getLatLong()">
        <input type="text" id= "city">
        <input id="btn" type="button" value="get Lat&Long" />
        <div class="push-down"></div>
    </body>
</html>


If you will run the above code, the result will output as =>

latLong[0]['lat'] = 19.2898089
latLong[0]['lng'] = 84.87185109999996
latLong[1]['lat'] = 19.2947663
latLong[2]['lng'] = 84.81182179999996

Hope this will help you :).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900