[Google Map]地址查詢(GClientGeocoder)
七月 29, 2008
地圖雖然可以隨意拖拉,但全世界那麼多怎樣才能最快找到目標呢?直接輸入地址查詢!
Google API
- GClientGeocoder : This class is used to communicate directly with Google servers to obtain geocodes for user specified addresses. In addition, a geocoder maintains its own cache of addresses, which allows repeated queries to be answered without a round trip to the server.
- .getLocations( address , callback ) 查詢地址。
- callback (result)
- result.Status.code == G_GEO_SUCCESS : 判斷是否成功。
- result.Placemark.length : 符合條件的位置數。
- result.Placemark[i].Point.coordinates[0]; : lng
- result.Placemark[i].Point.coordinates[1]; : lat
- result.Placemark[i].address; : 地址
程式碼
google.load("maps", "2.x");
google.setOnLoadCallback(initialize);
var map = null;
var geocoder = null;
function initialize() {
// 檢查瀏覽器是否可使用 Google Map API
if ( GBrowserIsCompatible() ) {
map = new google.maps.Map2(document.getElementById('map'));
geocoder = new GClientGeocoder();
// 設定地圖中心點
map.setCenter(new GLatLng(25.036772,121.520269), 12);
} // if
else {
alert('您的瀏覽器不支援Google Map');
} // else
}
function showLocation() {
// 清除marker
map.clearOverlays();
var address = document.getElementById('txtAddress').value;
geocoder.getLocations(address, cb_showLocation);
}
function cb_showLocation(result) {
// 顯示結果
if (result.Status.code == G_GEO_SUCCESS) {
// 成功
for (var i=0; i<result.Placemark.length; i++) {
var lat = result.Placemark[i].Point.coordinates[1]; // lat
var lng = result.Placemark[i].Point.coordinates[0]; // lng
var address = result.Placemark[i].address; // 地址
var point = new GLatLng(lat,lng);
var marker=new GMarker(point, {title:i+1});;
map.addOverlay(marker);
} // for
} // if
}
var status=[];
status[G_GEO_SUCCESS] = "Success";
status[G_GEO_MISSING_ADDRESS] = "Missing Address";
status[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address";
status[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address";
status[G_GEO_BAD_KEY] = "Bad Key";
status[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries";
status[G_GEO_SERVER_ERROR] = "Server Error";
[...] [Google Map]地址查詢(GClientGeocoder) [...]
google map 收錄於部觀門(Digest by door.urs.tw)…
這篇文章有很多有關 google map 的資訊, 因此收錄於部觀門精選文章 (This article got many useful information about google map, so we digest in BlogDoor system)…
非常感谢!我是一个初学者,您的代码很有参考价值!!
为什么显示时只能显示10条呢