我做了一些和你想达到的目标几乎相同的事情。我的解决方案工作得很好,但我用了完全不同的方式。
我的所有纬度和长度都存储在我的数据库中的地名等旁边,正如您所拥有的,但这是我处理它们的方法:
选择位置.php
<?php
include 'data.php';
mysql_connect($host, $user, $pass) or die ("Wrong Information");
mysql_select_db($db) or die("Wrong Database");
$result = mysql_query("SELECT * FROM reseller_addresses") or die ("Broken Query");
$letter = 'A';
while($row = mysql_fetch_array($result)){
$placeName = stripslashes($row['b_name']);
$placeCode = stripslashes($row['b_code']);
$placeTown = stripslashes($row['b_town']);
$places .= "<strong>$letter:</strong> $placeName, $placeTown, $placeCode<br>";
$placeLatLng = stripslashes($row['latlng']);
$hidden .= "$placeLatLng<br>";
$letter++;
}
mysql_close();
?>
索引文件
<?php
include 'private/selectPlaces.php'
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<link rel="stylesheet" type="text/css" href="hallets-style.css" />
<script type="text/javascript">
function initialize() {
var cent = new google.maps.LatLng(51.673606, -3.11542);
var myOptions = {
zoom: 10,
center: cent,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var locations = document.getElementById("hidden").innerHTML.toLowerCase();
var spLocations = locations.split("<br>");
var spLength = (spLocations.length)-1;
var letter = "A";
for(var i=0; i<spLength; i++){
var formLocations = spLocations[i].split(",");
var image = "http://www.google.com/intl/en_ALL/mapfiles/marker_black"+letter+".png";
var myLatLng = new google.maps.LatLng(formLocations[0], formLocations[1]);
var marker = new google.maps.Marker({
position: myLatLng,
icon: image,
map: map
});
letter = String.fromCharCode(letter.charCodeAt() + 1);
}
}
</script>
</head>
</head>
<body onload="initialize()">
<div id="wrapper">
<div id="left">
<div id="map_canvas"></div>
</div>
<div id="right">
<div id="menu">
<div
<div id="content"><?php echo $places; ?>
</div>
</div>
</div>
<div id="hidden" style="display:none"><?php echo $hidden; ?></div>
</body>
</html>
这里所发生的是我将所有坐标和位置输出到页面上的隐藏分区,然后由javascript通过getElementByID读取该分区。然后很容易将变量传递给Google标记代码。
希望这对你有帮助
干杯,
丹