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
分享到:
相关推荐
主要介绍了Google (Local) Search API的简单使用。需要的朋友可以过来参考下,希望对大家有所帮助
下面是源码: 代码如下:/* *Author:karry *Version:1.0 *Time:2008-12-01 *KMapSearch 类 *把GOOGLE MAP 和LocalSearch结合。只需要传入MAP\经纬度值,就可以把该经纬度附近的相关本地搜索内容取出来,在地图上标注...
在Google Search 方面具有比较详细的描述,包括blog search,book search,image search,local search等。。。
雇用本地-职位发布应用API 端回购该服务器由带有PostgreSQL数据库的Node和Express组成。 这是一个工作板应用程序,允许用户搜索和发布新工作。 用户可以上传,更新和删除简历。 他们可以保存工作以供日后查看以及...
实现本地搜索的控件,控件~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在每个项目的弹出模式下,您还将看到来自Google Search API的与该项目相关的链接。 同样,如果您想更新个人资料的电子邮件或密码,也可以通过“编辑个人资料”页面进行操作。 最终产品 入门 派生此存储库,然后克隆...
search for products on Amazon or Google. Chapter 8, Network Monitoring and Security, introduces you to various techniques for capturing, storing, analyzing, and manipulating network packets. This ...
* Fix conflict with plugin using Google Library Javascript for jQuery. Example : "Use Google Libraries" * Add an option for choose input text or textarea for old tags field * Add an option for min ...
Parsing a Local Mail Spool with mailbox 323 Try It Out: Printing a Summary of Your Mailbox 324 Fetching Mail from a POP3 Server with poplib 325 Try It Out: Printing a Summary of Your POP3 Mailbox ...
的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • ...
9.4. Zend_Date API Overview 9.4.1. Zend_Date Options 9.4.1.1. Selecting the date format type 9.4.1.2. DST and Date Math 9.4.1.3. Month Calculations 9.4.1.4. Speed up date localization and ...
(^- create this in an editor with some more or less intelligent Search抧扲eplace steps) 'Function Renamer' If you decompiled a file that was obfuscated all variable and function got lost. Is ...