google Map API实现地址解析
Google map API的地址解析服务目前是不收费的,可以将详细的地址转化成经纬度。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;
function initialize() {
geocoder = new google.maps.Geocoder();
}
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert(results[0].geometry.location);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
</head>
<body οnlοad="initialize()">
<div>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Geocode" οnclick="codeAddress()">
</div>
</body>
</html>
Google Geocoding API http://code.google.com/intl/zh-CN/apis/maps/documentation/geocoding/
地址解析请求
Google Geocoding API 请求必须采用以下形式:
http://maps.google.com/maps/api/geocode/output?parameters
其中,output
可以是以下值之一:
json
(建议)表示以 JavaScript 对象表示法 (JSON) 的形式输出xml
表示以 XML 的形式输出
有些参数是必需的,有些是可选的。根据网址的标准,所有参数均使用字符 & (&
) 分隔。下面枚举了这些参数及其可能的值。
Google Geocoding API 使用以下网址参数定义地址解析请求:
address
(必需)- 您要进行地址解析的地址。*或者
latlng
(必需)- 您希望获取的、距离最近的、可人工读取地址的纬度/经度文本值。*bounds
(可选)- 要在其中更显著地偏移地址解析结果的可视区域的边框。(有关详细信息,请参见下文的可视区域偏向。)region
(可选)- 区域代码,指定为 ccTLD(“顶级域”)双字符值。(有关详细信息,请参见下文的区域偏向。)language
(可选)- 传回结果时所使用的语言。请参见支持的区域语言列表。请注意,我们会经常更新支持的语言,因此该列表可能并不详尽。如果未提供language
,地址解析器将尝试尽可能使用发送请求的区域的本地语言。sensor
(必需)- 指示地址解析请求是否来自装有位置传感器的设备。该值必须为true
或false
。
* 请注意:您可以传递 address
或 latlng
进行查找。(如果传递 latlng
,则地址解析器执行反向地址解析。有关详细信息,请参阅反向地址解析。)
bounds
和 region
参数只会影响地址解析器返回的结果,但不能对其进行完全限制。
地址解析响应
地址解析响应将以网址请求路径中的 output
标记所指示的格式传回。
JSON 输出格式
在此示例中,Google Geocoding API 为针对“1600 Amphitheatre Parkway, Mountain View, CA”的查询请求一个 json
响应:
http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false
在此示例中,我们将 sensor
参数设为变量“true_or_false”以强调您必须将该值显式设置为 true
或 false
。
此请求传回的 JSON 如下所示。请注意,实际的 JSON 可能包含较少的空白。您不应假定请求之间的空白的数量或格式。
{ "status": "OK", "results": [ { "types": [ "street_address" ], "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "address_components": [ { "long_name": "1600", "short_name": "1600", "types": [ "street_number" ] }, { "long_name": "Amphitheatre Pkwy", "short_name": "Amphitheatre Pkwy", "types": [ "route" ] }, { "long_name": "Mountain View", "short_name": "Mountain View", "types": [ "locality", "political" ] }, { "long_name": "California", "short_name": "CA", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "United States", "short_name": "US", "types": [ "country", "political" ] }, { "long_name": "94043", "short_name": "94043", "types": [ "postal_code" ] } ], "geometry": { "location": { "lat": 37.4219720, "lng": -122.0841430 }, "location_type": "ROOFTOP", "viewport": { "southwest": { "lat": 37.4188244, "lng": -122.0872906 }, "northeast": { "lat": 37.4251196, "lng": -122.0809954 } } } } ] }
请注意,JSON 响应包含两个根元素:
"status"
包含请求中的元数据。请参见下文的状态代码。"results"
包含一个经过地址解析的地址信息和几何图形信息的数组。
通常,对于地址查询,只会在 "results"
数组中传回一个条目,但在进行模糊地址查询时,地址解析器可能会传回多个结果。
请注意,如果您希望从结果中提取值,则这些结果通常需要进行解析。解析 JSON 相对来说较容易。有关一些设计模式建议,请参见解析 JSON。
- 准备工作
要使用googleAPI 来开发自己的桌面应用程序,先要做下面的准备工作:
1. 首先在下面地址http://www.google.com/apis/download.html下载the Google Web APIs Developer's Kit。
2. 然后在下面地址https://www.google.com/accounts/NewAccount?continue=http://api.google.com/createkey&followup=http://api.google.com/createkey注册一个 license key 以使用google提供的搜索服务。
- 新建一个Google Search应用程序
1. 新建一个Windows From 项目命名为Google Search。
2. 添加Web引用,以便使用google web services。具体做法是将下载的the Google Web APIs Developer's Kit中的GoogleSearch.wsdl文件放入本地的服务器上,然后在VS中右键点击资源管理器中的引用,再选择添加Web引用,然后输入GoogleSearch.wsdl文件在本机服务器上的地址http://localhost/GoogleSearch.wsdl,更改Web引用名为GoogleSearch后,点击添加引用后返回。
3. 进行界面设计,添加3个控件,TextBox(用于输入关键字),Button(用于提交信息),RichTextBox(用于显示搜索结果信息)。如图:
4. 编写事件处理。
1) 添加命名空间。using Google_Search.googlesearch;
2) 双击Button控件,在Button的事件处理中添加下面的代码:
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
26
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/None.gif)
31
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
32
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
36
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/None.gif)
37
![](http://zmsx.cnblogs.com/Images/OutliningIndicators/None.gif)
3) 按ctrl+F5就能测试应用程序了。
l GoogleSearchServices类doGoogleSearch方法
需要进行搜索先建立一个GoogleSearchServices 类的对象,然后调用doGoogleSearch方法取得信息。下面是google提供的对该方法的描述。
public doGoogleSearch(string key, string q, int start, int maxResults, bool filter, string restrict, bool safeSearch, string lr, string ie, string oe)
下面对各个参数进行介绍:
Key:这是由google提供的一个认证用的ID,可以在http://www.google.com/apis/申请。由于目前是测试阶段,每个ID一天只提供1000次的搜索服务请求。
q:这个就是要搜索的关键字。string类型
start:结果开始的索引数,从0开始。 int 类型
maxresults:返回的结果数的最大值,最大为10。int 类型
filter:用于标识是否对搜索的结果进行过滤,所进行的过滤是对于同一主机上的内容只返回1—2个结果。bool类型
restrict:用于限定搜索的国家或地区,如果为空,表示不限制。string 类型。
下表将列出所有能选的代号。
国家 | 代号 | 国家 | 代号 | 国家 | 代号 | 国家 | 代号 |
Andorra | countryAD | Estonia | countryEE | Kazakhstan | countryKZ | Qatar | countryQA |
United Arab Emirates | countryAE | Egypt | countryEG | Lao People's Democratic Republic | countryLA | Reunion | countryRE |
Afghanistan | countryAF | Western Sahara | countryEH | Lebanon | countryLB | Romania | countryRO |
Antigua and Barbuda | countryAG | Eritrea | countryER | Saint Lucia | countryLC | Russian Federation | countryRU |
Anguilla | countryAI | Spain | countryES | Liechtenstein | countryLI | Rwanda | countryRW |
Albania | countryAL | Ethiopia | countryET | Sri Lanka | countryLK | Saudi Arabia | countrySA |
Armenia | countryAM | European Union | countryEU | Liberia | countryLR | Solomon Islands | countrySB |
Netherlands Antilles | countryAN | Finland | countryFI | Lesotho | countryLS | Seychelles | countrySC |
Angola | countryAO | Fiji | countryFJ | Lithuania | countryLT | Sudan | countrySD |
Antarctica | countryAQ | Falkland Islands (Malvinas) | countryFK | Luxembourg | countryLU | Sweden | countrySE |
Argentina | countryAR | Micronesia, Federated States of | countryFM | Latvia | countryLV | Singapore | countrySG |
American Samoa | countryAS | Faroe Islands | countryFO | Libyan Arab Jamahiriya | countryLY | St. Helena | countrySH |
Austria | countryAT | France | countryFR | Morocco | countryMA | Slovenia | countrySI |
Australia | countryAU | France, Metropolitan | countryFX | Monaco | countryMC | Svalbard and Jan Mayen Islands | countrySJ |
Aruba | countryAW | Gabon | countryGA | Moldova | countryMD | Slovakia (Slovak Republic) | countrySK |
Azerbaijan | countryAZ | United Kingdom | countryUK | Madagascar | countryMG | Sierra Leone | countrySL |
Bosnia and Herzegowina | countryBA | Grenada | countryGD | Marshall Islands | countryMH | San Marino | countrySM |
Barbados | countryBB | Georgia | countryGE | Macedonia, The FormerYugoslav Republic of | countryMK | Senegal | countrySN |
Bangladesh | countryBD | French Quiana | countryGF | Mali | countryML | Somalia | countrySO |
Belgium | countryBE | Ghana | countryGH | Myanmar | countryMM | Suriname | countrySR |
Burkina Faso | countryBF | Gibraltar | countryGI | Mongolia | countryMN | Sao Tome and Principe | countryST |
Bulgaria | countryBG | Greenland | countryGL | Macau | countryMO | El Salvador | countrySV |
Bahrain | countryBH | Gambia | countryGM | Northern Mariana Islands | countryMP | Syria | countrySY |
Burundi | countryBI | Guinea | countryGN | Martinique | countryMQ | Swaziland | countrySZ |
Benin | countryBJ | Guadeloupe | countryGP | Mauritania | countryMR | Turks and Caicos Islands | countryTC |
Bermuda | countryBM | Equatorial Guinea | countryGQ | Montserrat | countryMS | Chad | countryTD |
Brunei Darussalam | countryBN | Greece | countryGR | Malta | countryMT | French Southern Territories | countryTF |
Bolivia | countryBO | South Georgia and the South Sandwich Islands | countryGS | Mauritius | countryMU | Togo | countryTG |
Brazil | countryBR | Guatemala | countryGT | Maldives | countryMV | Thailand | countryTH |
Bahamas | countryBS | Guam | countryGU | Malawi | countryMW | Tajikistan | countryTJ |
Bhutan | countryBT | Guinea-Bissau | countryGW | Mexico | countryMX | Tokelau | countryTK |
Bouvet Island | countryBV | Guyana | countryGY | Malaysia | countryMY | Turkmenistan | countryTM |
Botswana | countryBW | Hong Kong | countryHK | Mozambique | countryMZ | Tunisia | countryTN |
Belarus | countryBY | Heard and Mc Donald Islands | countryHM | Namibia | countryNA | Tonga | countryTO |
Belize | countryBZ | Honduras | countryHN | New Caledonia | countryNC | East Timor | countryTP |
Canada | countryCA | Croatia (local name: Hrvatska) | countryHR | Niger | countryNE | Turkey | countryTR |
Cocos (Keeling) Islands | countryCC | Haiti | countryHT | Norfolk Island | countryNF | Trinidad and Tobago | countryTT |
Congo, The Democratic Republic of the | countryCD | Hungary | countryHU | Nigeria | countryNG | Tuvalu | countryTV |
Central African Republic | countryCF | Indonesia | countryID | Nicaragua | countryNI | Taiwan | countryTW |
Congo | countryCG | Ireland | countryIE | Netherlands | countryNL | Tanzania | countryTZ |
Switzerland | countryCH | Israel | countryIL | Norway | countryNO | Ukraine | countryUA |
Cote D'ivoire | countryCI | India | countryIN | Nepal | countryNP | Uganda | countryUG |
Cook Islands | countryCK | British Indian Ocean Territory | countryIO | Nauru | countryNR | United States Minor Outlying Islands | countryUM |
Chile | countryCL | Iraq | countryIQ | Niue | countryNU | United States | countryUS |
Cameroon | countryCM | Iran (Islamic Republic of) | countryIR | New Zealand | countryNZ | Uruguay | countryUY |
China | countryCN | Iceland | countryIS | Oman | countryOM | Uzbekistan | countryUZ |
Colombia | countryCO | Italy | countryIT | Panama | countryPA | Holy See (Vatican CityState) | countryVA |
Costa Rica | countryCR | Jamaica | countryJM | Peru | countryPE | Saint Vincent and the Grenadines | countryVC |
Cuba | countryCU | Jordan | countryJO | French Polynesia | countryPF | Venezuela | countryVE |
Cape Verde | countryCV | Japan | countryJP | Papua New Guinea | countryPG | Virgin Islands (British) | countryVG |
Christmas Island | countryCX | Kenya | countryKE | Philippines | countryPH | Virgin Islands (U.S.) | countryVI |
Cyprus | countryCY | Kyrgyzstan | countryKG | Pakistan | countryPK | Vietnam | countryVN |
Czech Republic | countryCZ | Cambodia | countryKH | Poland | countryPL | Vanuatu | countryVU |
Germany | countryDE | Kiribati | countryKI | St. Pierre and Miquelon | countryPM | Wallis and Futuna Islands | countryWF |
Djibouti | countryDJ | Comoros | countryKM | Pitcairn | countryPN | Samoa | countryWS |
Denmark | countryDK | Saint Kitts and Nevis | countryKN | Puerto Rico | countryPR | Yemen | countryYE |
Dominica | countryDM | Korea, Democratic People's Republic of | countryKP | Palestine | countryPS | Mayotte | countryYT |
Dominican Republic | countryDO | Korea, Republic of | countryKR | Portugal | countryPT | Yugoslavia | countryYU |
Algeria | countryDZ | Kuwait | countryKW | Palau | countryPW | South Africa | countryZA |
Ecuador | countryEC | Cayman Islands | countryKY | Paraguay | countryPY | Zambia | countryZM |
|
|
|
|
|
| Zaire | countryZR |
safeSearch:使用一个bool值表示是否过滤垃圾信息。
lr:对语言的限制,string类型。可选代号如下表:
语言 | 代号 |
| 语言 | 代号 |
Arabic | lang_ar |
| Icelandic | lang_is |
Chinese (S) | lang_zh-CN |
| Italian | lang_it |
Chinese (T) | lang_zh-TW |
| Japanese | lang_ja |
Czech | lang_cs |
| Korean | lang_ko |
Danish | lang_da |
| Latvian | lang_lv |
Dutch | lang_nl |
| Lithuanian | lang_lt |
English | lang_en |
| Norwegian | lang_no |
Estonian | lang_et |
| Portuguese | lang_pt |
Finnish | lang_fi |
| Polish | lang_pl |
French | lang_fr |
| Romanian | lang_ro |
German | lang_de |
| Russian | lang_ru |
Greek | lang_el |
| Spanish | lang_es |
Hebrew | lang_iw |
| Swedish | lang_sv |
Hungarian | lang_hu |
| Turkish | lang_tr |
ie:输入所使用的字符编码。这个参数将被忽略,所有的输入都将采用UTF-8。
oe:输出所使用的字符编码。这个参数将被忽略,所有的输出都将采用UTF-8
。
通过对这个方法的各个参数的设定可以是自己的应用程序在搜索方面更加人性化。具体使用时可以通过各种控件来收集这些信息。
l GoogleSearchResult类
这个类用于存储搜索结果,下面介绍这个类的一些重要属性:
1. estimatedTotalResultsCount 所有与关键字有关的信息的总数。
2. searchTime 提交信息到返回信息所用的时间。
3. searchTips 根据关键字提供一些与关键字相关的关键字,供选择。
4. resultElements 返回一个数组,用于存储返回结果的每一项。
l Result Element类
这个类的是搜索的结果中的每一项,所有的结果处理都依靠这个类。下面是这个类的一些属性的介绍:
1. URL 显示结果网页的url。
2. snippet 返回在目标网页中摘录一些包含关键字片段。
3. title 目标网页的标题。
4. hostname 该参数显示目标网页所在的主机明。
5. summary 如果目标网页在ODP 目录中存在,则返回摘要信息。
6. directoryTitle 显示ODP 目录的标题。
通过这两个类可以取得各种与查询有关的信息,通过以上的介绍,基本可以使用googleAPI提供的全部功能。