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 <DOCTYPE html>
2  <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
5 <title>Google Search API Sample</title>
6 <script src="http://www.google.com/jsapi?key=INSERT-YOUR-KEY"></script>
7 <script type="text/javascript">
8 // This code generates a "Raw Searcher" to handle search queries. The Raw Searcher requires
9   // you to handle and draw the search 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 for search 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 // Append search 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 the Local Search center point
48 localSearch.setCenterPoint("New York, NY");
49
50 // Set searchComplete as the callback function when a search is complete. The
51 // localSearch object will have results in it.
52 localSearch.setSearchCompleteCallback(this, searchComplete, null);
53
54 // Specify search quer(ies)
55 localSearch.execute('coffee New York NY');
56
57 // Include the required Google branding.
58 // Note that getBranding is 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 <body style="font-family: Arial;border: 0 none;">
68 <div id="branding" style="float: left;"></div><br />
69 <div id="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) {
}
}

使用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...

使用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...

今天先介绍到这里,以后我会更详细的进行简介一下。

下面是我用Flex做的一个示例,结合了Google Map,支持关键字搜索,并可以导出结果。

再次申明,程序只用于学习使用,请不要用于商业。需要安装Flash AIR

下载地址是:https://files.cnblogs.com/liongis/GMapLocalSearch.rar

posted on 2011-03-01 00:54  liongis  阅读(8576)  评论(8编辑  收藏  举报