Google Map Place API的初步使用

本帖最后由 菲菲 于 2012-6-19 13:43 编辑

如何使用Google Map Places API
http://www.paodot.net/?post=84

Google Places API: Supported Place Types
http://code.google.com/intl/zh-TW/apis/maps/documentation/places/supported_types.html

The Google Places API (Experimental)
http://code.google.com/intl/en/apis/maps/documentation/places/#PlaceSearchRequests

申请Google Map Place API Key的链接
https://code.google.com/apis/console/

终于得出最终结果:
https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyAiFpFd85eMtfbvmVNEYuNds5TEF9FjIPI&language=zh-CN

这里要注意:
1.要根据实际情况去测试,不能随便拿个参数去测试,这样肯定不能得到结果
2.关于key的问题,最好是需要自己去申请(翻墙申请)
3.关于language的问题,如果是强制性要求中文的话,最好是加上限制(目前暂时没有测试到有什么比较明显的区别)

谷歌官方文档:

where output may be either of the following values:

  
  • json (recommended) indicates output in JavaScript   Object Notation (JSON)
  • xml indicates output as XML
  

Certain parameters are required to initiate a Place Search request. As is standard in URLs, all parameters are separated using the ampersand (&) character. The list of parameters and their possible values are enumerated below.

  
  • key (required) — Your application's     API key. This key identifies     your application for purposes of quota management and so that Places     added     from your application are made immediately available to your app. Visit     the APIs Console to create an API Project and     obtain your key.
  • location (required) — The     latitude/longitude around which to retrieve Place     information. This must be specified as     latitude,longitude.
  • radius (required) — The distance     (in meters) within which to return Place results. The maximum allowed     radius is 50,000 meters.
  • sensor (required) — Indicates     whether or not the Place request came from a device using a location     sensor (e.g. a GPS) to determine the location sent in this request. This     value must be either true or false.

  • keyword (optional) — A term to be matched     against all available fields, including but not limited to name, type, and     address, as well as customer reviews and other third-party content.
  • language (optional) — The language code,     indicating in which language the results should be returned, if possible.     See the     list     of supported languages and their codes. Note that we often update     supported languages so this list may not be exhaustive.   
  • name (optional) — A term to be     matched against the names of Places. Results will be restricted to those     containing the passed name value.
  • types (optional) — Restricts the     results to Places matching at least one of the specified types. Types     should be separated with a pipe symbol (type1|type2|etc).     See the     list of       supported types.

前四个参数是必须填写,后面四个参数是参数可选的。

name的这个属性需要转编译码(encodeURI,encodeURIComponent)之后才能使用,在中国境内使用的应用程序,一般都会采用中文输入name值,所以需要转编译。(英文不需要,至于其他文字还没有尝试过)

下面是简单介绍三种编码区别:

转载自http://apps.hi.baidu.com/share/detail/21624627

JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 。

下面简单介绍一下它们的区别

1 escape()函数

定义和用法 
escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。

语法 
escape(string)

参数  描述  
string  必需。要被转义或编码的字符串。

返回值 
已编码的 string 的副本。其中某些字符被替换成了十六进制的转义序列。

说明 
该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。其他所有的字符都会被转义序列替换。


2 encodeURI()函数 
定义和用法 
encodeURI() 函数可把字符串作为 URI 进行编码。

语法 
encodeURI(URIstring)

参数  描述  
URIstring  必需。一个字符串,含有 URI 或其他要编码的文本。

返回值 
URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。

说明 
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。

该方法的目的是对 URI 进行完整的编码,因此对以下在 URI 中具有特殊含义的 ASCII 标点符号,encodeURI() 函数是不会进行转义的:;/?:@&=+$,#


3 encodeURIComponent() 函数

定义和用法 
encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。

语法 
encodeURIComponent(URIstring)

参数  描述  
URIstring  必需。一个字符串,含有 URI 组件或其他要编码的文本。

返回值 
URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。

说明 
该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。

其他字符(比如 :;/? :@&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。

提示和注释 
提示:请注意 encodeURIComponent() 函数 与 encodeURI() 函数的区别之处,前者假定它的参数是 URI  的一部分(比如协议、主机名、路径或查询字符串)。因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。


4 总结:

通过对三个函数的分析,我们可以知道:escape()除了 ASCII  字母、数字和特定的符号外,对传进来的字符串全部进行转义编码,因此如果想对URL编码,最好不要使用此方法。而encodeURI()  用于编码整个URI,因为URI中的合法字符都不会被编码转换。encodeURIComponent方法在编码单个URIComponent(指请求参 数)应当是最常用的,它可以讲参数中的中文、特殊字符进行转义,而不会影响整个URL。


在线编码工具:http://www.nengcha.com/code/url/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值