代码改变世界

Google map api 3.9 Service Geocoder 地理信息解析

2012-07-06 16:17  废墟中的垃圾  阅读(2722)  评论(0)    收藏  举报

Geocoder 地理解析相关服务对象

A service for converting between an address and a LatLng

这是一个提供 位置名称 和 经纬度 互相转换的服务。

构造函数 Geocoder() 用来创建一个 地理解系服务对象。在使用中实际上是 var geocoder = new google.maps.Geocoder();

方法:

geocode(request:GeocoderRequest, callback:function(Array.<GeocoderResult>, GeocoderStatus))  创建一个解析请求

现在来具体讲一下这个这个方法。

第一个参数是 GeocoderRequest ,这是一个地理信息服务请求对象接下来会具体的讲解。用于创建一个请求。

第二个参数是 function 也就是一个回调函数。 回调函数会返回两个参数,第一个参数是一个结果数组,第二个参数是解析状态(也是一个对象)

这里写一个 Demo :

var geocoder = new google.maps.Geocoder();

//要解析的地址
var address = "北京林业大学";

//创建请求对象
var geocoderRequest = {address : address};

geocoder.geocode( geocoderRequest , function(results, status) {
//判断解析状态
if (status == google.maps.GeocoderStatus.OK) {

//如果返回成功,把返回的第一个点设置为屏幕的中心点。

map.setCenter(results[0].geometry.location);

// 把返回的第一个点标记在地图上。
var marker = new google.maps.Marker({
map: map, 
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});

这个Demo程序的地址是 https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple

到这里地址解析成经纬度已经说的很明白了。当然有人说如果第一个点并不是真正需要的呢?

这里可以在回调函数里面进行操作。

其实只需要稍微修改一点

for(var i = 0;i< results.length;i++){
var marker = new google.maps.Marker({
map: map,
position: results[i].geometry.location
});
}

在标记外面加上一层循环就可以了。

 

GeocoderRequest 对象

The specification for a geocoding request to be sent to the Geocoder.

Geocoder 发送请求时的参数规范对象。(没有构造函数,不能用new的方法创建,可以直接用动态对象)

属性

address string 需要解析的地址
bounds LatLngBounds 要搜索的区域
location LatLng 需要反解析的经纬度
region string 国家代码顶级域

 

 

 

 

以上四个属性只需要传入一个就可以进行解析。

通过经纬度进行反向解析的Demo地址:https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-reverse

 

GeocoderStatus 对象 

The status returned by the Geocoder on the completion of a call to geocode().

在地理解系对象进行请求结束后返回的状态。

返回值有下面7种

ERROR 与Google服务器连接错误。
INVALID_REQUEST 非法的解析请求
OK  返回值包含成功解析的
OVER_QUERY_LIMIT 超出了查询频率,可以稍后重试
REQUEST_DENIED  这个网页不允许使用这个服务
UNKNOWN_ERROR  一个服务器错误导致了解系服务不能执行,重新执行可能会返回成功
ZERO_RESULTS  没有结果返回

 

 

 

 

 

 

GeocoderResult 对象

A single geocoder result retrieved from the geocode server. A geocode request may return multiple result objects. Note that though this result is "JSON-like," it is not strictly JSON, as it indirectly includes a LatLng object.

从服务器返回的单个请求结果。一个请求可以返回多个结果对象。返回的结果是一个类似于json的对象,但是不是严格意义上的json, 间接的包含了LatlLng 经纬度对象。

属性:

address_components Array.<GeocoderAddressComponent> 一个 GeocoderAddressComponent 的数组对象
formatted_address string 可读的地址
geometry GeocoderGeometry  一个 GeocoderGeometry 对象(这个最主要,用于解析)
types Array.<string>  提示解析返回的对象类型的数组

 

 

 

GeocoderAddressComponent  对象

A single address component within a GeocoderResult. A full address may consist of multiple address components.

解析结果里面的地址组件。一个完整的地址可能包含很多地址组件。

属性:

long_name  string  地址组件的全称
short_name  string  地址组件的段名称
types Array.<string> 这个地址组件的类型,例如locality 地区country 国家,很少会用到

 

 


GeocoderGeometry 对象

Geometry information about this GeocoderResult

解析结果的空间信息

属性:

bounds  LatLngBounds  如果可以确定,返回的结果的精确界限
location  LatLng  结果的经纬度坐标对象
location_type  GeocoderLocationType  返回结果的类型(GeocoderLocationType 对象里面会进行介绍)
viewport  LatLngBounds  推荐的可视界限

 

 

 

GeocoderLocationType 对象

Describes the type of location returned from a geocode.

描述解析返回结果的地点类型

类型值:

APPROXIMATE  近似的结果
GEOMETRIC_CENTER  几何中心,比如直线的中点或者正方形的中心等等。
RANGE_INTERPOLATED  返回的插值相对的精确结果
ROOFTOP  返回精确的结果

 

 

 

 

介绍到这里,已经把地点和经纬度解析的功能介绍完了,希望对某些人有一些帮助:)