steppermotor
steppermotor

Reputation: 701

Geocoding using Google API

I used this code to get latitude and longitude. I want to expand to multiple geo-code for multiple locations.

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

var geocoder = new google.maps.Geocoder(); var address = "new york";

geocoder.geocode( { 'address': address}, function(results, status) {

 if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude);   }  });  </script>

If I modify to, include two locations. Any ideas why this doesn't write to the webpage, any idea on how to store the latitude and longitude of multiple locations into an array.

<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>


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

var geocoder = new google.maps.Geocoder();
var address = [['New York City'],['Chicago, IL']];

geocoder.geocode( { 'address': address}, function(results, status) {

 if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
document.write(latitude);
document.write(longitude);
 }   
 }); 
</script>

</body>
</html> 

Upvotes: 0

Views: 196

Answers (1)

geocodezip
geocodezip

Reputation: 161324

See the documentation, address is a string, not an array of arrays of strings.

Note that the geocoder is subject to a quota and rate limit so the code below will only work for approximately 10 results without addressing status=OVER_QUERY_LIMIT.

var geocoder = new google.maps.Geocoder();
var address = [
    ['New York City'],
    ['Chicago, IL']
];
for (var i = 0; i < address.length; i++) {
    geocoder.geocode({
        'address': address[i][0]
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();
            document.getElementById('info').innerHTML += latitude + "<br>";
            document.getElementById('info').innerHTML += longitude + "<br>";
        } else {
            alert("geocode failed:"+status);
        }
    });
}

code snippet:

var geocoder = new google.maps.Geocoder();
var address = [
  ['New York City'],
  ['Chicago, IL']
];
for (var i = 0; i < address.length; i++) {
  geocoder.geocode({
    'address': address[i][0]
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var latitude = results[0].geometry.location.lat();
      var longitude = results[0].geometry.location.lng();
      document.getElementById('info').innerHTML += latitude + "<br>";
      document.getElementById('info').innerHTML += longitude + "<br>";
    } else {
      alert("geocode failed:" + status);
    }
  });
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="info"></div>

Upvotes: 1

Related Questions