0

I've got a database full of multiple addresses and I'm trying to mark all of them using Google Maps API. I've used this code before but it only lets my use one location at a time.

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps Geocoding Demo 1</title> 
   <script src="http://maps.google.com/maps/api/js?sensor=false" 
           type="text/javascript"></script> 
</head> 
<body> 
   <div id="map" style="width: 400px; height: 300px;"></div> 

   <script type="text/javascript"> 

   var address = 'London, UK';

   var map = new google.maps.Map(document.getElementById('map'), { 
       mapTypeId: google.maps.MapTypeId.TERRAIN,
       zoom: 6
   });

   var geocoder = new google.maps.Geocoder();

   geocoder.geocode({
      'address': address
   }, 
   function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
         new google.maps.Marker({
            position: results[0].geometry.location,
            map: map
         });
         map.setCenter(results[0].geometry.location);
      }
   });

   </script> 
</body> 
</html>
Alexander
  • 43
  • 5
  • 1
    How many addresses? Can you store the coordinates in the database? Have you searched for duplicates of your question? – geocodezip Jun 16 '19 at 12:56
  • I've got around 20 or so and I can only use city, post code and address. I found this code snippet above a while ago and it does what I want but I need to use more than one address. – Alexander Jun 16 '19 at 13:01
  • 20 is doable, but you will need to deal with the query rate limit. There are several duplicates here, I will try to dig one up when time permits. – geocodezip Jun 16 '19 at 13:09
  • Alright, thank you so much! – Alexander Jun 16 '19 at 13:15
  • Is there any way to increase the query rate limit by paying more or do I have to divide the request into many small ones if I get more addresses? Money isn't a problem for this. – Alexander Jun 16 '19 at 16:41
  • That is a Google terms of use or billing account question. I don't know, someone at Google could let you know. I know you can pay for additional queries, but I suspect that increasing the query rate might not be possible. – geocodezip Jun 16 '19 at 16:55

1 Answers1

0

You could use a PHP script to return the addresses from your database and then loop through in your Javascript.

function getAddresses() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            a = JSON.parse(this.responseText);
            placeMarkers(a);
        }
    };
    xhttp.open("GET", "getaddresses.php", true);
    xhttp.send();
}
function placeMarkers(a) {
    var geocoder = new google.maps.Geocoder();
    for(var x=0; x<a.length; x++) {
        geocoder.geocode({
            'address': a[x]
         }, 
         function(results, status) {
            if(status == google.maps.GeocoderStatus.OK) {
               new google.maps.Marker({
                  position: results[0].geometry.location,
                  map: map
               });
               map.setCenter(results[0].geometry.location);
            }
         });
    }
}

getaddresses.php

<?php 
$query = mysqli_query($link, "SELECT Address FROM AddressTable");
$addresses = [];
while($queryinfo = mysqli_fetch_array($query)) {
    array_push($addresses, $queryinfo['Address']);
}
$addresses = json_encode($addresses);
print $addresses;
?>

Then just call getAddresses()

Just tested on multiple addresses, and i'm getting an Over_Limit error after 10 queries.

Daz Chest
  • 66
  • 6
  • After ~10 results, this solution will encounter OVER_QUERY_LIMIT responses from the geocoder (and as it doesn't report failures, will silently fail). This problem has been around for years, and as such has multiple duplicates on the site. – geocodezip Jun 16 '19 at 15:13
  • @geocodezip The status does return "OVER_QUERY_LIMIT", so that could be tested i guess. – Daz Chest Jun 16 '19 at 15:29