0

I want to map a field, city, from a MySQL database into Google Maps. I found a nice script to do it, but haven't been able to get it to work.

If I hard code an array into locations (var locations = ), it works fine. Passing this SQL array into JS is giving me problems.

I've tried a number of different things, but I'm stuck. Right now I'm trying getElementById, but no dice. Any help is appreciated! Thanks.

<?php
$server_name="localhost";
$db_user="sql_user";
$db_pass="password";
$db="mydb";

$sql = "SELECT city FROM posts";
$result = mysql_query($query) or die ('Query Error: ' . mysql_error()); 
while ($results = mysql_fetch_array($result))
{
	$gender = $results[gender];
}

?>

  <div id="map_canvas" style="width: 600px; height: 400px"></div> 

  <script type="text/javascript"> 

    var locations = document.getElementById("$gender");

    var mapOpt = { 
       mapTypeId: google.maps.MapTypeId.TERRAIN,
       center: new google.maps.LatLng(38.00, -100.00),
       zoom: 3
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOpt);

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

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

          // Call the geocoder with a 100ms delay
          index++;
          if (locations.length > index) {
             setTimeout(geocoderFunction, 100);
          }
       });
    }

    // Launch the geocoding process
    geocoderFunction();
  </script>

Edited by kbomo: n/a

3
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by Airshow
0

write this

<?php
$server_name="localhost";
$db_user="sql_user";
$db_pass="password";
$db="mydb";

mysql_connect($server_name, $db_user, $db_pass);
mysql_select_db($db);

$sql = "SELECT city FROM posts";
$result = mysql_query($sql) or die ('Query Error: ' . mysql_error()); 
while ($results = mysql_fetch_array($result))
{
	$gender[] = $results;
}

?>

instead of

<?php
$server_name="localhost";
$db_user="sql_user";
$db_pass="password";
$db="mydb";

$sql = "SELECT city FROM posts";
$result = mysql_query($query) or die ('Query Error: ' . mysql_error()); 
while ($results = mysql_fetch_array($result))
{
	$gender = $results[gender];
}

?>

and also you cannot access a variable directly from php.

use Ajax or Use HTML Element attributes to access

0

Thanks...I fixed the while loop (I think) and am trying to grab $city from the PHP, but no go.

Any ideas what I have wrong here? Thanks!

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
  <title>Google Maps</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
</head> 
<body>

<?php
$server_name="localhost";
$db_user="sql_user";
$db_pass="password";
$db="mydb";

mysql_connect($server_name, $db_user, $db_pass);
mysql_select_db($db);

$sql = "SELECT city FROM posts";
$result = mysql_query($sql) or die ('Query Error: ' . mysql_error()); 
while ($results = mysql_fetch_array($result))
{
	$city[] = $results;
}

?>

  <div id="map_canvas" style="width: 600px; height: 400px"></div> 

  <script type="text/javascript"> 

    var locations = document.getElementById("<?=$city?>");

    var mapOpt = { 
       mapTypeId: google.maps.MapTypeId.TERRAIN,
       center: new google.maps.LatLng(38.00, -100.00),
       zoom: 3
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOpt);

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

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

          // Call the geocoder with a 100ms delay
          index++;
          if (locations.length > index) {
             setTimeout(geocoderFunction, 100);
          }
       });
    }

    // Launch the geocoding process
    geocoderFunction();
  </script> 
</body>
</html>
0

Got it! I was able to do a $city_text = ""; and then a <?=$city_text?>; to get it to pass the array correctly. Thanks.

0

You don't even need to use the intermediate php variable $city_text if you don't want to.

This should build the javascript statement in one line:

var locations = ['<?php echo implode("','", $city); ?>'];

Airshow

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.