在google map上顯示多點(從資料庫)

edited 十月 2013 in Javascript
我的問題是
addr陣列裡的值是我從資料庫所抓出來的地址
我利用for迴圈將地址一一標示在地圖上
但是有的地址會說無法顯示,,,我確定addr裡有抓到資料
但如果我自己輸入的話...還是找的到(自己有寫過簡單板的...但是地址要一個一個輸入..只是現在改成讀資料庫的資料)
不知道是哪裡出錯了..我是用php+mysql..有將地址轉成javascript型態
而且無法顯示的地址,,,有時候又找的到...(我總共有30筆資料..但有時候無法顯示的資料是10筆~20筆...不固定...但我資料是固定的)
我研究好久了..找不到錯在哪裡
請大家教教我> <,,,

以下是我的程式碼
<?php require_once('Connections/gmap_conn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

mysql_select_db($database_gmap_conn, $gmap_conn);
$query_rs_member = "SELECT * FROM member ORDER BY mIndex ASC";
$rs_member = mysql_query($query_rs_member, $gmap_conn) or die(mysql_error());
$row_rs_member = mysql_fetch_assoc($rs_member);
$totalRows_rs_member = mysql_num_rows($rs_member);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
<html xmlns="http://www.w3.org/1999/xhtml">;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Google Maps JavaScript API Example</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=AABQIAAAAnY2gXL6d9qXOjtcqCzmc0BT2yXp_ZAY8_ufC3CFXhHIE1NvwkxRe9TfCE1DdF7fnJ5jq-xB2wHmxbg&sensor=true_or_false";
type="text/javascript"></script>
<script type="text/javascript">
var addr = new Array();
var n1 = 0;
var map, geocoder, path, directionsPanel;
var directions ;//儲存畫圖物件
var markers = new Array();//儲存點座標
var count = 0; 
//HTML頁面載入時呼叫本函式
function initialize() {
//測試瀏覽器是否支援Google Maps API
if (GBrowserIsCompatible()) {
//建立GMap2物件
map = new GMap2(document.getElementById("map_canvas"));
//設定地圖中心,並初始化地圖物件
map.setCenter(new GLatLng(25.034738, 121.521449), 13);
//GClientGeocoder物件
geocoder=new GClientGeocoder();
//設定偏好搜尋國家/地區代碼
geocoder.setBaseCountryCode("TW")
map.setUIToDefault();


}
else {
alert("抱歉,此瀏覽器無法瀏覽Google Map");
}
}

/*function ShowMakers(){
for(var i=0; i<n1; i++){
ShowAddress(addr);
}
}*/
//ShowAddress函式功能:地址轉經緯度,並在地圖上做記號
function ShowAddress(){

if(geocoder){
for(var i=0; i<n1; i++){
//對地址進行解析座標的動作
//document.write(addr + "<br />");
geocoder.getLatLng(addr, function(latlng){
//解析失敗
if(!latlng){
window.alert("不能解析:"+i);
}else{
//將解析的座標設為地圖中心
map.setCenter(latlng, 13);
//加入GMaker物件
var marker=new GMarker(latlng);
map.addOverlay(marker);
//將座標存入陣列中
markers[count] = latlng;
//加入地圖資訊視窗
//marker.openInfoWindowHtml(addr);

count++;
}
}
);
}
}

}

function Routing(){
directionsPanel = document.getElementById("route");
directions = new GDirections(map, directionsPanel);
directions.loadFromWaypoints(markers);
}
</script>
</head>

<body onload="initialize()" onunload="GUnload()">


<table width="723" border="1" align="center">
<tr>
<td width="173">姓名</td>
<td width="176">電話</td>
<td width="352">地址</td>
</tr>
<?php do {?>
<tr>
<td><?php echo $row_rs_member; ?></td>
<td><?php echo $row_rs_member; ?></td>
<td><?php echo $row_rs_member; ?></td>
</tr>

<script type="text/javascript">
addr[n1] = "<?php echo $row_rs_member; ?>";

//document.write(addr[n1]+"<br />");
n1++;


</script>
<?php } while ($row_rs_member = mysql_fetch_assoc($rs_member));?>
</table>
<p>
<input type="button" value="資料點" onclick="ShowAddress()"/>
<input type="button" value="路徑規劃" onclick="Routing()"/>
</p>
<table>
<tr>
<td width=700>
<div id="map_canvas" style="width: 700px; height: 600px; border:1px solid black"></div>
</td>
<td width=400>
<div id="route" style="width: 400px; height: 600px; overflow:auto; border:1px solid black"></div>
</td>
</tr>
</table>
</body>
</html>
<?php
mysql_free_result($rs_member);
?>

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=4185

評論

Sign In or Register to comment.