惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
H
Help Net Security
雷峰网
雷峰网
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
GbyAI
GbyAI
博客园_首页
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
I
InfoQ
小众软件
小众软件
Google DeepMind News
Google DeepMind News
D
Docker
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog

博客园 - 废墟中的垃圾

Android 学习笔记一 -- 环境搭建 DOTNET网站中级程序员招聘 同时招聘其他职位 windows 2003 和 windows xp 下安装 Windows Phone Developer Tools RTW(sdk) 微博关注其实图标可以自己定义:) Google map api 3.9 Directions 相关的服务 Google map api 3.9 Service 服务相关 pmropn.exe 暂用cpu 的解决办法 免费百度刷下拉 刷联想词软件,共享出来,大家共勉 商城SEO - 当前关键词的设定 真正解决方案:无法启动调试--未安装 Silverlight Developer 运行时。请安装一个匹配版本。 SEO研究中心第三版优化教程(扫描版)目录添加版 framework4.0 预先需要安装的系统组件 SQLServer ERRORLOG 删除 老魏帮忙发的木棒问题的最大长度优先匹配原则算法 CodeSmith 学习之数据库遍历数据库表的所有字段 Google map api v3 Services —— Elevation 在 Google map api v3 里面使用 海拔对象 Google Map —— The Google Elevation API Google map v3 发布 关于 server.urlencode 中文乱码的问题 asp 和 .net 都说明一下
Google map api 3.9 Service Geocoder 地理信息解析
废墟中的垃圾 · 2012-07-06 · via 博客园 - 废墟中的垃圾

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

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  返回精确的结果

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