Wakeeta
Wakeeta

Reputation: 57

Google Maps with PHP/mySQL

Basically I'm having a problem with getting google maps to tag addresses that are in my database, when I use php to do:

print codeAddress("example address");

it works fine, however, when I set it up as I have it here it doesn't even display the map, can anybody here help me with this issue?

Thanks, Wakeeta

<DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
  src="http://maps.googleapis.com/maps/api/js?key=AIzaSyAWiB6PqOyqsJJZLmoZ5CFb2IP6sqqhtI8&sensor=false">
    </script>
    <script type="text/javascript">
  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
<?php
    $dbh=mysql_connect('webdb.uvm.edu','swakita','PASSWORD');

if (!$dbh)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db('SWAKITA', $dbh);

$addressprint = mysql_query("SELECT fldStreet FROM tblLocation");
while ($row = mysql_fetch_row($addressprint)) {
foreach ($row as $field) {
print "codeAddress($field)";
}
}
?>
 }



  function codeAddress(address) {
    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);
      }
    });
  }
</script>
</head>
<body onload="initialize()">

<div id="map_canvas" style="height:90%;top:30px"></div>
</body>
</html>

Upvotes: 0

Views: 1032

Answers (1)

zinzendorf
zinzendorf

Reputation: 120

print "codeAddress($field)";

creates invalid Javascript code because when PHP expands the $field variable it will not be in quotes. This prevents the map from displaying because the Javascript encounters a fatal error. Try this:

print "codeAddress(\"$field\")";

Upvotes: 1

Related Questions