<?php
$id = $_GET[\'id\'];
// ....撈出這筆 id 的地址 過程略
$address = $book[\'address\'];
$map_api_key = 申請的 google maps API;
$smarty->assign(\'google_maps_js\', load_google_maps($map_api_key, $address));
function load_google_maps($map_api_key, $address) {
$gmap_js = \'
<!-- Google Maps Mashup -->
<script type="text/javascript" src="http://www.google.com/jsapi?key=\' ... "></script>
<script type="text/javascript">
google.load("maps", "2", {"locale" : "zh_TW"}); // 引入 maps api 、設定介面語系為正體中文
function initialize() {
//if (GBrowserIsCompatible()) { 判斷瀏覽器是否支援 google maps
var map = new google.maps.Map2(document.getElementById("map"));
var geocoder = new GClientGeocoder(); // 地理編碼解譯
var address = "\'.$address.\'";
map.addControl(new GSmallMapControl());
map.addControl(new GOverviewMapControl());
geocoder.getLatLng(address, function(point) {
if (!point) {
alert("Google Maps 找不到該地址,無法顯示地圖!"); // 如果Google Maps無法顯示該地址的警示文字
} else {
map.setCenter(point, 16);
/* 暫訂 */
var marker = createMarker(point, "\'.$address.\'");
map.addOverlay(marker);
var bounds = map.getBounds();
// 西南座標
var southWest = bounds.getSouthWest();
// 東北座標
var northEast = bounds.getNorthEast();
// 地圖四角座標
var lng1 = northEast.lng();
var lng2 = southWest.lng();
var lat1 = northEast.lat();
var lat2 = southWest.lat();
// 除錯
//document.getElementById("debug").innerHTML = "xml.php?lng1="+ lng1 +"&lng2="+ lng2 +"&lat1="+ lat1 +"&lat2="+ lat2;
GDownloadUrl("xml.php?lng1="+ lng1 +"&lng2="+ lng2 +"&lat1="+ lat1 +"&lat2="+ lat2, function(data, responseCode) {
if(responseCode == 200) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new GLatLng(lat,lng);
var title = markers[i].getAttribute("title");
//var address = markers[i].getAttribute("title");
//var title = markers[i].getAttribute("title");
var marker = createMarker(point, title);
map.addOverlay(marker);
}
} else if(responseCode == -1) {
alert("Data request timed out. Please try later.");
} else {
alert("Request resulted in error. Check XML file is retrievable.");
}
});
}
});
}
function createMarker(point, title) {
var marker = new GMarker(point);
var html = "<p><small>" + title + "</small></p>";
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
google.setOnLoadCallback(initialize);
</script>
\';
return $gmap_js;
}
$smarty->display(\'show_map.html\');
?>
|