国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Google (Local) Search API的簡單使用介紹

2019-11-20 21:34:15
字體:
來源:轉載
供稿:網友

花了兩天的時間來用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()

設置Search Service(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);

•new google.search.DrawOptions();

設置Google Search Control的顯示方式

•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對象。

復制代碼 代碼如下:

function LocalSearchKeepHandler(result) {
  var from = 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"});
 function initialize() {
     //LocalSearch Object used to create a local search service for the maps
        var localSearch = new google.search.LocalSearch(); 
        //restrict the local search resutls to kml and geocode results only, no business ones
        localSearch.setRestriction(google.search.Search.RESTRICT_TYPE, google.search.LocalSearch.TYPE_KMLONLY_RESULTS);            
        // Set the Local Search center point
  localSearch.setCenterPoint("北京"); 
  //It's about local search, which are used to set where the results will appear, a param of options
  var resultCanvas = document.getElementById("resultCanvas");      
  //options: open, alternate root
  var searcherOptions = new google.search.SearcherOptions();
  //show many results
  searcherOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
  //no results message
  searcherOptions.setNoResultsString(google.search.SearchControl.NO_RESULTS_DEFAULT_STRING);
  //options.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);//web, local... in a tab show
  searcherOptions.setRoot(resultCanvas); //show the results in another place--<div id="resultCanvas">
     //SearchControl Object used to create a search service which will include a local search service                  
  var searchControl = new google.search.SearchControl(null);
  searchControl.addSearcher(localSearch, searcherOptions); 
  searchControl.addSearcher(new google.search.WebSearch());
  searchControl.addSearcher(new google.search.NewsSearch());
  searchControl.addSearcher(new google.search.BlogSearch());
  //draw options and set it to a tabbed view,
  var drawOptions = new google.search.DrawOptions();
  drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED)
  //make the searchControl return a result:GResult
  searchControl.setOnKeepCallback(this, LocalSearchKeepHandler);//keeping a search result
  //this option is used to set the search box position in a DOM tree.
  //drawOptions.setSearchFormRoot(document.getElementById("drawOptions"));
  //set the input box to a user defined element 
  //drawOptions.setInput(document.getElementById("input"));
  // tell the search box to draw itself and tell it where to attach
 // searchControl.draw(document.getElementById("searchBox"), drawOptions);//Here I changed fromaddress and toaddress to search, a new place
  //another user defined input box
  drawOptions.setInput(document.getElementById("input2"));
  searchControl.draw();
  /** The codes below is about google Ajax Map Search API
  //this code segment is used to add a sidebar to show the results of the search
  //I wonder why no 'var' exists here
   optinos = new Object();
   options.resultList = resultCanvas;
   options.resultFormat = "multi-line1";
   var lsc2 = new google.elements.LocalSearch(options);
   map.addControl(lsc2, new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(-282, -2)));
    */
        }
 google.setOnLoadCallback(initialize);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北川| 巴林右旗| 衡南县| 长顺县| 车险| 仁布县| 娱乐| 阿拉善右旗| 威信县| 额尔古纳市| 桂林市| 个旧市| 玉溪市| 荔浦县| 北宁市| 平凉市| 南靖县| 达拉特旗| 上思县| 西吉县| 九寨沟县| 沙洋县| 灯塔市| 确山县| 韩城市| 岑巩县| 乌鲁木齐市| 白银市| 溧水县| 即墨市| 霍林郭勒市| 秭归县| 鲁山县| 灵璧县| 梨树县| 田林县| 五华县| 绵竹市| 旬邑县| 子洲县| 海阳市|