Google(Local)SearchAPI的简单使用介绍
花了两天的时间来用Google的API来做这么一个小东西,其实真正的实现代码不是很多,十几行而已。费时间的工作是了解各个API的功能以及调试JavaScript。
•构造函数google.search.LocalSearch()
这其实是创建了一个LocalSearch的Service,这个Service和其他Service(News,Blog,Web)一样,是供SearchControl使用的。这些Service决定了SearchControl的能力。
•设置LocalSearch的搜索结构类型
localSearch.setRestriction(google.search.Search.RESTRICT_TYPE,google.search.LocalSearch.TYPE_KMLONLY_RESULTS)
这说明搜索的结果没有business的结果,只有kml和geocode结果
•设置LocalSearch的搜索范围
localSearch.setCenterPoint("北京");
•google.search.SearcherOptions()
设置SearchService(Searcher)的属性,作为SearchControl.addSearcher()的一个属性使用,有以下选项可以选择:
1.设置结果的显示方式
•searcherOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
2. 设置档没有搜索结果时显示的文字
• searcherOptions.setNoResultsString(google.search.SearchControl.NO_RESULTS_DEFAULT_STRING);
3. 设置结果显示的位置
•searcherOptions.setRoot(resultCanvas);
•newgoogle.search.DrawOptions();
设置GoogleSearchControl的显示方式
•drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED)
设置显示方式为tabbed方式,即各个Searcher像tabs一样显示
•drawOptions.setInput(document.getElementById("input"));
将搜索输入框的默认值改为用户自定义的一个输入框
将用户选择搜索结果作为一个相应的GResult对象返回,如LocalSearch的GResult就是一个GLocalResult。
这个选项费了我很长时间才找到,原因有二,一是用的人少,文档少。二是我看的英文文档,花了挺长时间才看明白,其实看中文文档要花的时间更长,我觉得。
•searchControl.setOnKeepCallback(this,LocalSearchKeepHandler);
顺便贴上LocalSearchKeepHandler的代码,其参数为自动返回的那个GResult对象。
functionLocalSearchKeepHandler(result){
varfrom=document.getElementById("from");
alert("result.tilte="+result.title);
from.value=ProcessString(result.title);
alert("from.value="+from.value);
// alert(result.title);
}
干脆把这段代码整体贴出,方便阅读
google.load("search","1",{"language":"zh-CN"});
functioninitialize(){
//LocalSearchObjectusedtocreatealocalsearchserviceforthemaps
varlocalSearch=newgoogle.search.LocalSearch();
//restrictthelocalsearchresutlstokmlandgeocoderesultsonly,nobusinessones
localSearch.setRestriction(google.search.Search.RESTRICT_TYPE,google.search.LocalSearch.TYPE_KMLONLY_RESULTS);
//SettheLocalSearchcenterpoint
localSearch.setCenterPoint("北京");
//It"saboutlocalsearch,whichareusedtosetwheretheresultswillappear,aparamofoptions
varresultCanvas=document.getElementById("resultCanvas");
//options:open,alternateroot
varsearcherOptions=newgoogle.search.SearcherOptions();
//showmanyresults
searcherOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
//noresultsmessage
searcherOptions.setNoResultsString(google.search.SearchControl.NO_RESULTS_DEFAULT_STRING);
//options.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);//web,local...inatabshow
searcherOptions.setRoot(resultCanvas); //showtheresultsinanotherplace--<divid="resultCanvas">
//SearchControlObjectusedtocreateasearchservicewhichwillincludealocalsearchservice
varsearchControl=newgoogle.search.SearchControl(null);
searchControl.addSearcher(localSearch,searcherOptions);
searchControl.addSearcher(newgoogle.search.WebSearch());
searchControl.addSearcher(newgoogle.search.NewsSearch());
searchControl.addSearcher(newgoogle.search.BlogSearch());
//drawoptionsandsetittoatabbedview,
vardrawOptions=newgoogle.search.DrawOptions();
drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED)
//makethesearchControlreturnaresult:GResult
searchControl.setOnKeepCallback(this,LocalSearchKeepHandler);//keepingasearchresult
//thisoptionisusedtosetthesearchboxpositioninaDOMtree.
//drawOptions.setSearchFormRoot(document.getElementById("drawOptions"));
//settheinputboxtoauserdefinedelement
//drawOptions.setInput(document.getElementById("input"));
//tellthesearchboxtodrawitselfandtellitwheretoattach
// searchControl.draw(document.getElementById("searchBox"),drawOptions);//HereIchangedfromaddressandtoaddresstosearch,anewplace
//anotheruserdefinedinputbox
drawOptions.setInput(document.getElementById("input2"));
searchControl.draw();
/**ThecodesbelowisaboutgoogleAjaxMapSearchAPI
//thiscodesegmentisusedtoaddasidebartoshowtheresultsofthesearch
//Iwonderwhyno"var"existshere
optinos=newObject();
options.resultList=resultCanvas;
options.resultFormat="multi-line1";
varlsc2=newgoogle.elements.LocalSearch(options);
map.addControl(lsc2,newGControlPosition(G_ANCHOR_TOP_LEFT,newGSize(-282,-2)));
*/
}
google.setOnLoadCallback(initialize);
相关文章
- 无需多视图!Google重磅升级NeRF:仅需一张平面图即可生成3D模型
- Google Earth Engine学习资料汇总与分享
- Google浏览器清除缓存怎么弄_谷歌浏览器f12清理缓存
- Google黑客基础语法学习与使用
- Google搜索引擎的使用技巧「建议收藏」
- Google Cloud免费使用一年以及无限续费的教程说明
- com.google.android.xmppService与com.google.android.gtalkservice
- 使用 Google Breakpad 来助力解决程序崩溃
- 【Google Play】Google Play 签名维护 ( 签名机制 | Google Play 签名机制选择 | 签名更新 )
- 使用 Google 字体加速服务,加快 WordPress 打开速度
- 使用 Google Analytics 分析 WordPress 博客的活跃用户
- Google Friend Connect for WordPress 插件
- 使用 Google 浏览器 Chrome 的扩展
- Google Hosted Libraries:使用 Google 服务器给开源 JavaScript 库加速
- 统一备份体验:谷歌开始力推Backup by Google One服务
- 微软正测试适用于Xbox的新版Edge 或可运行Google Stadia
- [下载] 谷歌浏览器Google Chrome v83.0.4103.61正式版(稳定版)现已发布
- [下载] Google Chrome v87正式版发布 带来多项新功能及重大性能改进
- Google 开源了世界上最准确的语言解析器 Parsey McParseface
- Google 知道你点击了哪个网页
- #新闻拍一拍# Firefox 探索没有 Google 的未来
- Android Google Map V2,提供MapFragment,可以与使用Fragment一样,使用Google Map了
- Google Chrome vs 微软Edge:哪款浏览器更快?
- 独家探秘Google无人驾驶汽车:神秘的红色按钮(组图)
- Google Chrome vs 微软Edge:哪款浏览器更快?
- 谷歌新Tango和一体机引入“视觉定位服务”,实现厘米级定位 | Google I/O 2017
- Google Maps美国地区可发现和使用Spin的电动自行车/滑板车
- Jquery模仿Baidu、Google搜索时自动补充搜索结果提示