簡體   English   中英

使用php / javascript從mysql數據庫在google map上顯示標記

[英]Displaying markers on google map from mysql database using php/javascript

在我的數據庫中,我有經度/經度值,並且正在嘗試在Google地圖上顯示標記。 (我的地圖可以正常顯示,但標記無法顯示!)。

 // 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!"
});   

}                

任何幫助將非常感謝!

我認為您需要使用“ echo”:

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

當您開始從PHP編寫javascript時,它變得非常復雜,因為它很容易在所有分號中丟失。

我也發現用PHP而不是不斷地跳入HTML-PHP-HTML-PHP等更加容易

嘗試這個

// 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>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM