Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle Maps (api v3) - Loop through mysql query results to geocode and place multiple markers on map
    primarykey
    data
    text
    <p>I have a google map on a page of search results that show up to 20 listings. The 20 listings are pulled from a mysql query and looped on the page. That part is working fine.</p> <p>However, the google map (which also geocodes the addresses for me), only shows the first listing. I'm at a loss how to make it loop through each listing and put them all on the map.</p> <p>I'm sure it will all easy to follow and understand once I get it working, but I just don't have the chops to get it working from scratch. Google's developer docs haven't been much help either....</p> <p>Any help immensely appreciated!!</p> <p><em><strong></em>**<em>*</em>**<em>*</em>**<em>*</em></strong> UPDATE TO QUESTION <strong><em>*</em>**<em>*</em>**<em>*</em></strong></p> <p>The answer provided I couldn't see to get working....the problem was that before the fix, the page showed all the listings, but only mapped 1. After implementing the answer, the page only shows the first listing and the map doesn't show up at all.</p> <p>I made a trimmed down A &amp; B version. This is basically a full paste in of the entire page code:</p> <p>VERSION 1: This shows all listings on the page, but only maps the first listing:</p> <pre><code>&lt;?php mysql_select_db($database_db, $db); $query_my_query = "SELECT * FROM biz WHERE state = 'MO' and city = 'springfield' order by name asc limit 0,5 "; $my_query = mysql_query($query_my_query, $db) or die(mysql_error()); $row_my_query = mysql_fetch_assoc($my_query); $totalRows_my_query = mysql_num_rows($my_query); $address = $row_my_query['address']; $city = $row_my_query['city']; $state = $row_my_query['state']; ?&gt; &lt;script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; var geocoder; var map; function initialize() { geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(34.052234,-118.243685); var address = '&lt;?php echo $address.', '.$city.', '.$state; ?&gt;'; var myOptions = { zoom: 14, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); geocoder.geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); } else { alert("Geocode was not successful for the following reason: " + status); } }); } &lt;/script&gt; &lt;head&gt; &lt;title&gt;TEST&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" &gt; &lt;/head&gt; &lt;body onload="initialize()"&gt; &lt;div&gt; &lt;?php do { // horizontal looper?&gt; &lt;?php echo $row_my_query['name'].' '.$row_my_query['address'].' '.$row_my_query['city'].', '.$row_my_query['state']; ?&gt; &lt;?php echo substr($row_my_query['zip'],0,5); ?&gt;&lt;br&gt; &lt;?php $row_my_query = mysql_fetch_assoc($my_query); } while ($row_my_query); //end horizontal looper ?&gt; &lt;/div&gt; &lt;div id="map_canvas" style="width: 300px; height: 300px;"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>HERE IS V2: This only ends up showing 1 listing on the page and the map doesn't work at all:</p> <pre><code>&lt;?php mysql_select_db($database_db, $db); $query_my_query = "SELECT * FROM biz WHERE state = 'MO' and city = 'springfield' order by name asc limit 0,5 "; $my_query = mysql_query($query_my_query, $db) or die(mysql_error()); $row_my_query = mysql_fetch_assoc($my_query); $totalRows_my_query = mysql_num_rows($my_query); $address = $row_my_query['address']; $city = $row_my_query['city']; $state = $row_my_query['state']; ?&gt; &lt;script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; var geocoder; var map; function initialize() { geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(34.052234,-118.243685); var addresses = []; &lt;?php while ($row = mysql_fetch_assoc($my_query)) { ?&gt; addresses.push(&lt;?php echo '"' . $row['address'].', '.$row['city'].', '.$row['state'] . '"'; ?&gt;); &lt;?php } ?&gt; var myOptions = { zoom: 14, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); for(i in addresses) { var address = addresses[i]; geocoder.geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); } else { // alert("Geocode was not successful for the following reason: " + status); } }); } &lt;/script&gt; &lt;head&gt; &lt;title&gt;TEST&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" &gt; &lt;/head&gt; &lt;body onload="initialize()"&gt; &lt;div&gt; &lt;?php do { // horizontal looper?&gt; &lt;?php echo $row_my_query['name'].' '.$row_my_query['address'].' '.$row_my_query['city'].', '.$row_my_query['state']; ?&gt; &lt;?php echo substr($row_my_query['zip'],0,5); ?&gt;&lt;br&gt; &lt;?php $row_my_query = mysql_fetch_assoc($my_query); } while ($row_my_query); //end horizontal looper ?&gt; &lt;/div&gt; &lt;div id="map_canvas" style="width: 300px; height: 300px;"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Please help!! I cannot for the life of me figure out how to resolve this problem, but I know the answer must be right in front of me somehow.</p> <p>Thanks as always....</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload