2008年11月15日星期六

Google地圖服務開發

本來過去是使用Urmap來進行地圖服務開發測試,但是由於應該是商業模式的考量,所以一直不支援地址反查經緯度(Geocoding),而後Google map推出api,不但有各國地圖,且重要的是支援Geocoding,另外在衛星地圖方面也更為清楚,所以Urmap真的差太多了。

而一般地圖服務一定要用的Geocoding的api使用起來也很簡單。

1.申請一個網站使用的key


2.基本流程是送個request到maps.google.com/maps/geo,可以請它回傳經緯度,可以xml,kml,json的格式。(參考官方文件)

Ex:以下用asp做範例(地址要用URLEncode)
location=getHTTPPage("http://maps.google.com/maps/geo?q=" + Server.URLEncode(address) + "&key=xxx&output=xml")

function getHTTPPage(surl)
IF Len(sURL) <> 0 Then
Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
Set xmldom = Server.CreateObject("Microsoft.XMLDOM")

xmlhttp.open "Post",sURL,false
xmlhttp.send()
html = bytes2BSTR(xmlhttp.responseBody)
getHTTPPage=html
Else
getHTTPPage="e"
End IF
end function

Function bytes2BSTR(vIn)
Dim i, ThischrCode, NextchrCode
strReturn = ""
For i = 1 To LenB(vIn)
ThischrCode = AscB(MidB(vIn, i, 1))

If ThischrCode < &H80 Then
strReturn = strReturn & Chr(ThischrCode)
Else
NextchrCode = AscB(MidB(vIn, i + 1, 1))
strReturn = strReturn & Chr(CLng(ThischrCode) * &H100 + CInt(NextchrCode))
i = i + 1
End If
Next
num=InStrRev(strReturn,"<coordinates")
if num > 0 then
strReturn=Mid(strReturn,num+13)
le=Len(strReturn)
le=le-53
strReturn=Mid(strReturn,1,le)

bytes2BSTR = strReturn
else
bytes2BSTR="e"
end if
End Function

我是使用文字取出的方式,當然你可以用parse xml的方式找到Tag coordinates的經緯度值。

p.s 過去大陸地區是沒開放Geocoding不過最近好像開放了!

3.你可以用所申請的key在你本機測試

在網站上引用Google map
1.台灣,香港
script src="http://maps.google.com.tw/maps?file=api&v=2&key=xxx" type="text/javascript"
2.大陸
script src="http://ditu.google.com/maps?file=api&v=2&key=xxx" type="text/javascript"

詳細細節可以參考官方文件

在手機上使用靜態地圖

img src="http://maps.google.com/staticmap?&maptype=mobile&key=xxx&markers=25.029226,121.520445%7C25.029226,121.520445&center=,&size=110x100&zoom=11"
zoom:設定比例
size:設定大小
center:設定中心
makers:設定標誌

詳細細節可以參考官方文件