Google Local Search API 简介
来源:互联网 发布:软件研发部门职责 编辑:程序博客网 时间:2024/05/17 04:13
GoogleLocal Search API 简介
Google 提供了一个基于javascript的本地搜索的API,我们可以通过这个API来嵌入到我们的应用程序中,实现搜索的功能。如javascrtip,Flash,java等。
此接口返回的数据为JSON格式的数据,可以方便进行解析。
Google Local Search API首页地址是:
http://code.google.com/intl/zh-CN/apis/maps/documentation/localsearch/index.html
以下是一个简单的例子:
1 <DOCTYPEhtml>
2 <htmlxmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <metahttp-equiv="content-type" content="text/html; charset=utf-8"/>
5 <title>GoogleSearch API Sample</title>
6 <scriptsrc="http://www.google.com/jsapi?key=INSERT-YOUR-KEY"></script>
7 <scripttype="text/javascript">
8 // This code generates a "Raw Searcher" to handlesearch queries. The Raw Searcher requires
9 // you to handle and draw thesearch results manually.
10 google.load('search','1');
11
12 var localSearch;
13 function searchComplete() {
14
15 // Check that we got results
16 document.getElementById('content').innerHTML='';
17 if (localSearch.results&& localSearch.results.length>0) {
18 for (var i=0; i< localSearch.results.length; i++) {
19
20 // Create HTML elements forsearch results
21 var p= document.createElement('p');
22 var a= document.createElement('a');
23 var b= document.createElement('b');
24 var c= document.createElement('c');
25 a.href = localSearch.results[i].url;
26 a.innerHTML = localSearch.results[i].title;
27 b.innerHTML ="<br>"+
28 localSearch.results[i].streetAddress;
29 c.innerHTML ="<br>"+
30 localSearch.results[i].city +","+
31 localSearch.results[i].region;
32
33 // Appendsearch results to the HTML nodes
34 p.appendChild(a);
35 p.appendChild(b);
36 p.appendChild(c);
37 document.body.appendChild(p);
38 }
39 }
40 }
41
42 function onLoad() {
43
44 // Create a LocalSearch instance.
45 localSearch =new google.search.LocalSearch();
46
47 // Set theLocal Search center point
48 localSearch.setCenterPoint("New York, NY");
49
50 // Set searchComplete as the callback function when asearch is complete. The
51 // localSearch object will have results in it.
52 localSearch.setSearchCompleteCallback(this, searchComplete,null);
53
54 // Specifysearch quer(ies)
55 localSearch.execute('coffee New York NY');
56
57 // Include the required Google branding.
58 // Note that getBrandingis called on google.search.Search
59 google.search.Search.getBranding('branding');
60 }
61
62 // Set a callback to call your code when the page loads
63 google.setOnLoadCallback(onLoad);
64
65 </script>
66 </head>
67 <bodystyle="font-family: Arial;border: 0 none;">
68 <divid="branding" style="float: left;"></div><br/>
69 <divid="content">Loading...</div>
70 </body>
71 </html>
2 <htmlxmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <metahttp-equiv="content-type" content="text/html; charset=utf-8"/>
5 <title>GoogleSearch API Sample</title>
6 <scriptsrc="http://www.google.com/jsapi?key=INSERT-YOUR-KEY"></script>
7 <scripttype="text/javascript">
8 // This code generates a "Raw Searcher" to handlesearch queries. The Raw Searcher requires
9 // you to handle and draw thesearch results manually.
10 google.load('search','1');
11
12 var localSearch;
13 function searchComplete() {
14
15 // Check that we got results
16 document.getElementById('content').innerHTML='';
17 if (localSearch.results&& localSearch.results.length>0) {
18 for (var i=0; i< localSearch.results.length; i++) {
19
20 // Create HTML elements forsearch results
21 var p= document.createElement('p');
22 var a= document.createElement('a');
23 var b= document.createElement('b');
24 var c= document.createElement('c');
25 a.href = localSearch.results[i].url;
26 a.innerHTML = localSearch.results[i].title;
27 b.innerHTML ="<br>"+
28 localSearch.results[i].streetAddress;
29 c.innerHTML ="<br>"+
30 localSearch.results[i].city +","+
31 localSearch.results[i].region;
32
33 // Appendsearch results to the HTML nodes
34 p.appendChild(a);
35 p.appendChild(b);
36 p.appendChild(c);
37 document.body.appendChild(p);
38 }
39 }
40 }
41
42 function onLoad() {
43
44 // Create a LocalSearch instance.
45 localSearch =new google.search.LocalSearch();
46
47 // Set theLocal Search center point
48 localSearch.setCenterPoint("New York, NY");
49
50 // Set searchComplete as the callback function when asearch is complete. The
51 // localSearch object will have results in it.
52 localSearch.setSearchCompleteCallback(this, searchComplete,null);
53
54 // Specifysearch quer(ies)
55 localSearch.execute('coffee New York NY');
56
57 // Include the required Google branding.
58 // Note that getBrandingis called on google.search.Search
59 google.search.Search.getBranding('branding');
60 }
61
62 // Set a callback to call your code when the page loads
63 google.setOnLoadCallback(onLoad);
64
65 </script>
66 </head>
67 <bodystyle="font-family: Arial;border: 0 none;">
68 <divid="branding" style="float: left;"></div><br/>
69 <divid="content">Loading...</div>
70 </body>
71 </html>
其中最重要的是调用这个地址:
http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=Palm%20Springs%20CA
两个必须的参数如下:
v:版本号,如1.0
q:搜索的关键字
还有一些其它常可以用到的参数:
key:搜索的时候,需要验证的key值,这个你必须到google上去申请
sll:中心坐标,你可以指定一个坐标为中心进行搜索
rsz:每页显示几条数据,值为1-8,当然,每次搜索最大记录数为64
我们来看看常见的几种语言是如何来使用的:
使用Flash
var service:HTTPService =new HTTPService();
service.url ='http://ajax.googleapis.com/ajax/services/search/local';
service.request.v ='1.0';
service.request.q ='Palm%20Springs%20CA';
service.request.key ='INSERT-YOUR-KEY';
service.resultFormat ='text';
service.addEventListener(ResultEvent.RESULT, onServerResponse);
service.send();
private function onServerResponse(event:ResultEvent):void {
try {
var json:Object = JSON.decode(event.result as String);
// now have some fun with the results...
} catch(ignored:Error) {
}
}
service.url ='http://ajax.googleapis.com/ajax/services/search/local';
service.request.v ='1.0';
service.request.q ='Palm%20Springs%20CA';
service.request.key ='INSERT-YOUR-KEY';
service.resultFormat ='text';
service.addEventListener(ResultEvent.RESULT, onServerResponse);
service.send();
private function onServerResponse(event:ResultEvent):void {
try {
var json:Object = JSON.decode(event.result as String);
// now have some fun with the results...
} catch(ignored:Error) {
}
}
使用Java
URL url =new URL("http://ajax.googleapis.com/ajax/services/search/local?"+
"v=1.0&q=barack%20obama&key=INSERT-YOUR-KEY&userip=INSERT-USER-IP");
URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer",/* Enter the URL of your site here*/);
String line;
StringBuilder builder =new StringBuilder();
BufferedReader reader =new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line= reader.readLine())!= null) {
builder.append(line);
}
JSONObject json =new JSONObject(builder.toString());
// now have some fun with the results...
"v=1.0&q=barack%20obama&key=INSERT-YOUR-KEY&userip=INSERT-USER-IP");
URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer",/* Enter the URL of your site here*/);
String line;
StringBuilder builder =new StringBuilder();
BufferedReader reader =new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line= reader.readLine())!= null) {
builder.append(line);
}
JSONObject json =new JSONObject(builder.toString());
// now have some fun with the results...
使用PHP
$url ="http://ajax.googleapis.com/ajax/services/search/local?"+
"v=1.0&q=barack%20obama&key=INSERT-YOUR-KEY&userip=INSERT-USER-IP";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_REFERER,/* Enter the URL of your site here*/);
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
"v=1.0&q=barack%20obama&key=INSERT-YOUR-KEY&userip=INSERT-USER-IP";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_REFERER,/* Enter the URL of your site here*/);
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
今天先介绍到这里,以后我会更详细的进行简介一下。
下面是我用Flex做的一个示例,结合了Google Map,支持关键字搜索,并可以导出结果。
再次申明,程序只用于学习使用,请不要用于商业。需要安装Flash AIR
下载地址是:http://files.cnblogs.com/liongis/GMapLocalSearch.rar
分享到:
- Google Local Search API 简介
- Google Local Search API 简介
- Google Local Search API 简介
- Google (Local) Search API的简单使用
- JSON and Google Local Search
- Google Search API Worms
- Google Web Search API 实现
- 使用 Google Ajax Search API
- 使用Google Ajax Search API
- Google AJAX Search API 密钥!
- Google Web Search API (Deprecated)
- 使用google custom search api
- java调用Google Search API
- [Google API](1)简介
- Google API 简介
- Local Search
- An API for Google Image Search
- 新的Google Ajax Search Api
- 使用4种不同的方式访问数组中的元素
- 长时间整理的xhtml网页设计规范
- android混淆使用
- 黑马程序员---迭代器Iterator的使用及其相关方法
- 防止vps主机被别人绑定域名解析
- Google Local Search API 简介
- 提示sql_server_服务不存在或拒绝访问
- JQuery模拟拖拽
- oracle XML DB的使用
- poj 2502 Subway dijkstra基础 !!!!入门题
- VC6编译的程序发生ESP错误
- tomcat连接数设置
- android handle
- 按钮图片表单显示不对齐