作者kk08744 (sexyboy)
看板Ajax
標題[問題] google map 與路徑規劃相關問題
時間Sat Sep 12 23:35:55 2009
各位前輩好,小弟最近在寫google map的相關網頁程式,遇到一些問題,希望能有前輩
相助。
關於google mpa路經規劃API,其Maps API Directions Illustrated
google的demo網站為:
http://snipurl.com/rs0kc
語法主要使用GDirections.load()
小弟嘗試更改其程式碼將setDirections("San Francisco", "Mountain View", "en_US");
改為setDirections("板橋火車站", "台北火車站", "zh-TW");
跑不出結果,小弟以為google map不提供路徑規劃的中文輸入
後來我另外的想法是利用geocoding查詢兩地的經緯度
directions.load("from: 25.001169323785923, 121.45265579223633 to:
25.03341222278933, 121.56749725341797");
直接輸入經緯度是可以的,但是我修改過的程式,變數最後一直顯示NaN,想請各位大大
幫忙解惑
------------------------------以下為程式碼----------------------------------
<script type="text/javascript">
var map = null;
var geocoder = null;
var marker;
var directionsPanel;
var directions;
var geoPoint=new GLatLng;
var i=0;
var geoPoint1=new GLatLng;
var geoPoint2=new GLatLng;
function initialize()
{
if (GBrowserIsCompatible())
{
map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl()); //加入地圖縮放工具
map.addControl(new GMapTypeControl()); //加入地圖切換的工具
map.addMapType(G_PHYSICAL_MAP); //加入地形圖
map.setCenter(new GLatLng(25.001689, 121.460809), 8); //設定台灣為中心點
geocoder = new GClientGeocoder();
directionsPanel = document.getElementById("my_textual_div");
directions = new GDirections(map, directionsPanel);
}
}
function createMarker(point,title,html)
{
var marker = new GMarker(point);
GEvent.addListener(marker, "click", function()
{
marker.openInfoWindowHtml(
html,
{
maxContent: html,
maxTitle: title}
);
});
return marker;
}
function showAddress(address1,address2)
{
geoPoint1=make(address1);
geoPoint2=make(address2);
alert(geoPoint);
directions.load("from: " +geoPoint1 + "to: " + geoPoint2, "zh-TW");
}
function make(test)
{
var testPoint=new GLatLng;
if (geocoder)
{
geocoder.getLatLng(
test,
function(point)
{
if (!point)
{
alert(test + " not found");
}
else
{
if(marker) //移除上一個點
{
//map.removeOverlay(marker);
}
map.setCenter(point, 13);
var title = "地址";
//line(point,i);
marker = createMarker(point,title,test);
map.addOverlay(marker);
marker.openInfoWindowHtml(test,{maxContent: test,maxTitle: title});
testPoint=point;
alert(testPoint);
}
}
);
}
return testPoint;
}
</script>
----------------------------------程式碼結束---------------------------------
測試網頁連結:
http://192.192.155.86/geocoding1.htm
小弟剛接觸JavaScript不久,希望各位大大能幫忙,小弟程度不好,如果問題很簡單,
請指點迷津,小弟先說聲抱歉~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.169.10.152
1F:推 guancheng:GDirections.load()的第二個參數好像是要用json來傳送吧 09/14 03:55
2F:→ guancheng:{ "locale": "zh_TW" } 試看看這樣吧~~ 09/14 03:58
3F:→ guancheng:ps. 其實我也沒用過~只是看了一好文件好像是這樣@@a 09/14 04:02
4F:→ kk08744:樓上大大~我改成 09/14 10:18
5F:→ kk08744:directions.load("from: 銝緍憭批飛 to: 啣貗怨뮊 09/14 10:18
6F:→ kk08744:directions.load("from: 世新大學 to: 台北火車站", 09/14 10:20
7F:→ kk08744:{ "locale": "zh_TW" }); 09/14 10:20
8F:→ kk08744:依然不行... 09/14 10:20
9F:推 guancheng:剛剛去申請了key來試了一下~程式沒有問題,是地址的問題 09/14 20:53
10F:→ guancheng:我用"from: 世新大學 to: 台北火車站"也是不行的 09/14 20:54
11F:→ guancheng:但是用完整地址卻可以,忽然想到加了個Taiwan給他吧 09/14 20:55
13F:→ guancheng:我直接把Taiwan加在送出的程式裡了 09/14 20:58
14F:→ guancheng:這樣直接輸入"世新大學"跟"台北火車站"也找的到 09/14 20:59
15F:→ kk08744:感謝大大^^~ 09/15 07:51
16F:推 nick9063937:請guancheng大大 放上你測試的完整code 11/27 10:28
17F:→ nick9063937:因為我也遇到一樣的問題 11/27 10:28