XQuery/Google 地理編碼
外觀
< XQuery
您有一個或多個地理名稱,並且想要建立這些位置的地圖。
我們將使用 Google RESTful 網路服務從地名列表中返回地理資料。Google 提供了一個基於 HTTP 的 地理編碼服務。這需要為 API 金鑰註冊一個站點,並且對使用此 API 存在限制。結果可以以 XML 或 JSON 格式返回。
以下指令碼接受一個位置並返回來自服務的 xml
let $key := "apikey"
let $location := request:get-parameter("location",())
let $location := escape-uri($location,false())
let $url := concat("https://maps.googleapis.com/maps/api/geocode/xml?address=",$location,"&key=",$key)
let $response := doc($url)
return
$response
</source >
==== Examples ====
Single City Examples:
* [http://kitwallace.co.uk/xqbook/geo/googlegeocode.xq?location=Minneapolis Minneapolis] - example using a single city with no country
* [http://kitwallace.co.uk/xqbook/geo/googlegeocode.xq?location=Bristol,UK Bristol,UK] - example using a city and country
Multiple matches may be returned:
* [http://kitwallace.co.uk/xqbook/geo/googlegeocode.xq?location=Bristol Bristol]
* or a false positive: [http://kitwallace.co.uk/xqbook/geo/googlegeocode.xq?location=Santas+House Santa's House]
In the UK, this service will geocode postcodes:
* [http://kitwallace.co.uk/xqbook/geo/googlegeocode.xq?location=BS3+4EA Bristol Hackspace]
== Response as KML ==
The XML response can be reformated as a KML file. Note the addition of the relevant media-type for KML .
<syntaxhighlight lang="xml">
let $key := "my apikey"
let $location := request:get-parameter("location",())
let $location := escape-uri($location,false())
let $url := concat("https://maps.googleapis.com/maps/api/geocode/xml?address=",$location,"&key=",$key)
let $response := doc($url)/GeocodeResponse
return
if ($response/status="OK")
then
let $x := response:set-header('Content-disposition',concat('inline;filename="',$location,'.kml";'))
let $result := $response/result[1]
let $serialize := util:declare-option("exist:serialize", "method=xml media-type=application/vnd.google-earth.kml+xml indent=yes")
return
<kml>
<Folder>
<name>{$location}</name>
{for $result in $response/result
return
<Placemark>
<name>{string($result/formatted_address)}</name>
<Point>
<coordinates>{$result/geometry/location/lng/string()},{$result/geometry/location/lat/string()},0</coordinates>
</Point>
</Placemark>
}
</Folder>
</kml>
else $response
如果您有 GoogleEarth,它應該載入一個疊加層,顯示多個布里斯托爾位置:布里斯托爾 KML
檢視生成的 kml 的一種簡單方法是使用 GoogleMaps。此指令碼只需構建使用上述指令碼的相關 URL,然後重定向到該 URL
let $location := request:get-parameter("location",())
let $wikiurl := escape-uri(concat("http:/kitwallace.co.uk/geocodekml.xq?location=",$location),false())
let $url := concat("http://maps.google.co.uk/maps?q=",$wikiurl)
return
response:redirect-to(xs:anyURI($url))
遺憾的是,這種方法不再適用於 GoogleMap - 請參閱 支援丟失 布里斯托爾
另一種方法是使用 GoogleMap 的 JavaScript API。請注意,要嵌入 JavaScript 需要將 { 複製,因此通常將 JavaScript 放入單獨的檔案中更容易。這種方法利用任何現有的 kml 疊加層,更簡單,但比使用 JavaScript API 實現地圖更受限制
declare option exist:serialize "method=xhtml media-type=text/html";
let $location := request:get-parameter("location",())
let $url := escape-uri(concat("http://kitwallace.co.uk/xqbook/geo/geocodekml.xq?location=",$location),false())
return
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3&key=myapikey"></script>
<script type="text/javascript">
function initialize(url) {{
var map = new google.maps.Map(document.getElementById("map_canvas"));
var kmlLayer = new google.maps.KmlLayer({{
url: url,
map:map
}});
}}
</script>
</head>
<body onload="initialize('{$url}')">
<h2>Map of {$location}</h2>
<div id="map_canvas" style="height: 80%;width: 80%; margin: 0px; padding: 0px">
</div>
</body>
</html>
* Bristol * London
[設定縮放級別似乎不起作用]
通常,位置在資料檔案中定義。以下指令碼讀取一個本地檔案(威士忌酒廠的樣本)並建立一個 KML 疊加層。
let $key := "apikey"
let $locations := doc("/db/apps/xqbook/geo/whisky.xml")
let $serialize := util:declare-option("exist:serialize", "method=xml media-type=application/vnd.google-earth.kml+xml")
let $x := response:set-header('Content-disposition','inline;filename=whisky.kml')
return
<kml>
<Folder>
<name>Distilleries</name>
{for $location in $locations//Whisky
let $url := concat("https://maps.googleapis.com/maps/api/geocode/xml?address=",$location/Postcode,"&key=",$key)
let $response := doc($url)/GeocodeResponse
let $result := $response/result[1]
return
<Placemark>
<name>{$location/Brand/string()}</name>
<description>{$location/Address/string()}</description>
<Point>
<coordinates>{$result/geometry/location/lng/string()},{$result/geometry/location/lat/string()},0</coordinates>
</Point>
</Placemark>
}
</Folder>
</kml>
以及 JavaScript 的修改版本,可以使用輸出建立 威士忌地圖