Flyingis

Fusion Center Lab.

Google Geocoding

    作者:Flyingis

    地理信息最重要的信息就是地理位置,如何能快速有效的定位到我们所期望的地方,是每一个GIS系统需要认真考虑的问题,根据属性定位空间位置和选择空间位置查询属性信息,成为现在绝大多数GIS系统的基本功能。

    同样是位置查询,Geocoding的功能更贴近我们的生活,它可以将具体的地址信息转换为空间坐标点(X/Y),Reverse Geocoding则是将一个空间坐标点转换为具体的地址信息。Geocoding规则建立需要城市街道命名规范、完整,是中国城市所欠缺的,需要逐步完善。ArcGIS很早就提供了对Geocoding支持,现在Google也实现了相似的功能,包括Geocoding和Reverse Geocoding,加上Google Map/Earth,Google在逐步成为一个面向大众网络GIS基础服务中心。

    GClientGeocoder对象的getLatLng和getLocations分别实现Geocoding和Reverse Geocoding。

    Geocoding

var map = new GMap2(document.getElementById("map_canvas"));
var geocoder = new GClientGeocoder();
function showAddress(address) {
    geocoder.getLatLng(
        address,
        
function(point) {
            
if (!point) {
                alert(address 
+ " not found");
            } 
else {
                map.setCenter(point, 
13);
                
var marker = new GMarker(point);
                map.addOverlay(marker);
                marker.openInfoWindowHtml(address);
            }
        }
    );
}


    Reverse Geocoding

var map;
var geocoder;
var address;
function initialize() {
    map 
= new GMap2(document.getElementById("map_canvas"));
    map.setCenter(
new GLatLng(40.730885,-73.997383), 15);
    map.addControl(
new GLargeMapControl);
    GEvent.addListener(map, 
"click", getAddress);
    geocoder 
= new GClientGeocoder();
}
function getAddress(overlay, latlng) {
    
if (latlng != null) {
        address 
= latlng;
        geocoder.getLocations(latlng, showAddress);
    }
}
function showAddress(response) {
    map.clearOverlays();
    
if (!response || response.Status.code != 200) {
        alert(
"Status Code:" + response.Status.code);
    } 
else {
        place 
= response.Placemark[0];
        point 
= new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
        marker 
= new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(
            
'<b>orig latlng:</b>' + response.name + '<br/>' + 
            
'<b>latlng:</b>' + place.Point.coordinates[0+ "," + place.Point.coordinates[1+ '<br>' + 
            
'<b>Status Code:</b>' + response.Status.code + '<br>' + 
            
'<b>Status Request:</b>' + response.Status.request + '<br>' + 
            
'<b>Address:</b>' + place.address + '<br>' + 
            
'<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' + 
            
'<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
    }
}

    Reverse Geocoding定位的具体地址并非精确的结果,而是满足一定容差值的地址结果集,ArcGIS Geocode可以设置结果的正确率,如仅列举出符合程度为60%及以上的地址,如果没有搜索到结果,Google返回G_GEO_UNKNOWN_ADDRESS (602),Geocoding结果保存在客户端cache中,加快执行相同geocoding时的速度,具体方法为GClientGeocoder.setCache()。

    上述方法都是在客户端执行geocoding操作,除了使用GClientGeocoder之外,还可以通过HTTP执行geocoding,方便服务器端的调试:


    To access the Maps API Geocoder, send a request to http://maps.google.com/maps/geo? with the following parameters in the URI:

    q (required) — The address that you want to geocode.
    key (required) — Your API key.
    output (required) — The format in which the output should be generated. The options are xml, kml, csv, or (default) json.
    ll (optional) — The {latitude,longitude} of the viewport center expressed as a comma-separated string (e.g. "ll=40.479581,-117.773438" ). This parameter only has meaning if the spn parameter is also passed to the geocoder. 
    spn (optional) — The "span" of the viewport expressed as a comma-separated string of {latitude,longitude} (e.g. "spn=11.1873,22.5" ). This parameter only has meaning if the ll parameter is also passed to the geocoder.
    gl (optional) — The country code, specified as a ccTLD ("top-level domain") two-character value.
    Note: The gl and spn,ll viewport parameters will only influence, not fully restrict, results from the geocoder.

    In this example, we request the geographic coordinates of Google's headquarters:
    http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=xml&key=abcdefg

posted on 2008-11-09 12:45  Flyingis  阅读(2500)  评论(6编辑  收藏  举报

导航