使用php/javascript从mysql数据库中显示在谷歌映射上的标记。

时间:2023-01-05 22:22:07

In my database I have lat/lng values and I'm trying to display markers on a google map. (I have my map coming up ok but can't get the markers going!).

在我的数据库中,我有lat/lng值,我试图在谷歌地图上显示标记。(我的地图还可以,但是我不能让标记继续下去!)

 // Selects all the rows in the tester table.
$query = 'SELECT * FROM tester WHERE 1';
$result = mysql_query($query);

if (!$result) 
{
die('Invalid query: ' . mysql_error());
}

    while ($row = mysql_fetch_assoc($result))
    {?>
       var marker = new google.maps.Marker({
    position: <?php $row['lat']?>, <?php $row['lng']?> ,
    map: map,
    title:"Hello World!"
});   

}                

Any help would be great thanks!

任何帮助都很好,谢谢!

2 个解决方案

#1


1  

I think you need to use "echo":

我认为你需要使用“echo”:

position: <?php echo $row['lat']?>, <?php echo $row['lng']?> ,

#2


1  

It gets quite complex when you start writing javascript out from PHP as its very easy to get lost in all the semi-colons.

当您开始从PHP编写javascript时,它变得非常复杂,因为它很容易在所有的半冒号中丢失。

Also I find it easier to do it all in PHP rather than keep jumping in and out of HTML-PHP-HTML-PHP etc

而且,我发现在PHP中完成所有这些操作比在HTML-PHP-HTML-PHP中来回跳转更容易

Try this

试试这个

// Selects all the rows in the tester table.
$query = 'SELECT * FROM tester WHERE 1';
$result = mysql_query($query);

if (!$result) {
    die('Invalid query: ' . mysql_error());
}

echo '<script type="text/javascript">';

$i = 0;
while ($row = mysql_fetch_assoc($result))
    echo 'var myLatlng = new google.maps.LatLng(' . $row['lat'] ',' . $row['lng'] . ');';

    echo 'var marker' . $i . ' = new google.maps.Marker({';
    echo '  position: myLatLng ,';
    echo '  map: map,';
    echo '  title:"Hello World!"';
    echo '});';
    echo 'marker' . $i . 'setMap(map);';
    $i++;
}               


echo '<script>';

#1


1  

I think you need to use "echo":

我认为你需要使用“echo”:

position: <?php echo $row['lat']?>, <?php echo $row['lng']?> ,

#2


1  

It gets quite complex when you start writing javascript out from PHP as its very easy to get lost in all the semi-colons.

当您开始从PHP编写javascript时,它变得非常复杂,因为它很容易在所有的半冒号中丢失。

Also I find it easier to do it all in PHP rather than keep jumping in and out of HTML-PHP-HTML-PHP etc

而且,我发现在PHP中完成所有这些操作比在HTML-PHP-HTML-PHP中来回跳转更容易

Try this

试试这个

// Selects all the rows in the tester table.
$query = 'SELECT * FROM tester WHERE 1';
$result = mysql_query($query);

if (!$result) {
    die('Invalid query: ' . mysql_error());
}

echo '<script type="text/javascript">';

$i = 0;
while ($row = mysql_fetch_assoc($result))
    echo 'var myLatlng = new google.maps.LatLng(' . $row['lat'] ',' . $row['lng'] . ');';

    echo 'var marker' . $i . ' = new google.maps.Marker({';
    echo '  position: myLatLng ,';
    echo '  map: map,';
    echo '  title:"Hello World!"';
    echo '});';
    echo 'marker' . $i . 'setMap(map);';
    $i++;
}               


echo '<script>';