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
以下是一个简单的例子:
<DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>Google Search API Sample</title> <script src="http://www.google.com/jsapi?key=INSERT-YOUR-KEY"></script> <script type="text/javascript"> // This code generates a "Raw Searcher" to handle search queries. The Raw Searcher requires // you to handle and draw the search results manually. google.load('search', '1'); var localSearch; function searchComplete() { // Check that we got results document.getElementById('content').innerHTML = ''; if (localSearch.results && localSearch.results.length > 0) { for (var i = 0; i < localSearch.results.length; i++) { // Create HTML elements for search results var p = document.createElement('p'); var a = document.createElement('a'); var b = document.createElement('b'); var c = document.createElement('c'); a.href = localSearch.results[i].url; a.innerHTML = localSearch.results[i].title; b.innerHTML = "<br>" + localSearch.results[i].streetAddress; c.innerHTML = "<br>" + localSearch.results[i].city + "," + localSearch.results[i].region; // Append search results to the HTML nodes p.appendChild(a); p.appendChild(b); p.appendChild(c); document.body.appendChild(p); } } } function onLoad() { // Create a LocalSearch instance. localSearch = new google.search.LocalSearch(); // Set the Local Search center point localSearch.setCenterPoint("New York, NY"); // Set searchComplete as the callback function when a search is complete. The // localSearch object will have results in it. localSearch.setSearchCompleteCallback(this, searchComplete, null); // Specify search quer(ies) localSearch.execute('coffee New York NY'); // Include the required Google branding. // Note that getBranding is called on google.search.Search google.search.Search.getBranding('branding'); } // Set a callback to call your code when the page loads google.setOnLoadCallback(onLoad); </script> </head> <body style="font-family: Arial;border: 0 none;"> <div id="branding" style="float: left;"></div><br /> <div id="content">Loading...</div> </body> </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
下载地址是:http://files.cnblogs.com/liongis/GMapLocalSearch.rar
?
?
本文转自:http://www.cnblogs.com/liongis/archive/2011/03/01/1967593.html