欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

百度地圖API之本地搜索與范圍搜索

 更新時(shí)間:2015年07月30日 17:16:33   投稿:mrr  
地圖服務(wù)是指可以提供數(shù)據(jù)信息的接口,比如說(shuō)本地搜索/路線規(guī)劃等,下面介紹下百度地圖API之本地搜索與范圍搜索的相關(guān)知識(shí),有需要的小伙伴可以來(lái)學(xué)習(xí)下

地圖服務(wù)是指可以提供數(shù)據(jù)信息的接口,比如說(shuō)本地搜索/路線規(guī)劃等,下面小編給大家整理下百度地圖API之本地搜索和范圍搜索,具體請(qǐng)看下文。

地圖服務(wù)概述
  地圖服務(wù)是指那些提供數(shù)據(jù)信息的接口,比如本地搜索、路線規(guī)劃等等。百度地圖API提供的服務(wù)有:

LocalSearch:本地搜索,提供某一特定地區(qū)的位置搜索服務(wù),比如在北京市搜索“公園”。
TransitRoute:公交導(dǎo)航,提供某一特定地區(qū)的公交出行方案的搜索服務(wù)。
DrivingRoute:駕車導(dǎo)航,提供駕車出行方案的搜索服務(wù)。
WalkingRoute:步行導(dǎo)航,提供步行出行方案的搜索服務(wù)。
Geocoder:地址解析,提供將地址信息轉(zhuǎn)換為坐標(biāo)點(diǎn)信息的服務(wù)。
LocalCity:本地城市,提供自動(dòng)判斷您所在城市的服務(wù)。
TrafficControl:實(shí)時(shí)路況控件,提供實(shí)時(shí)和歷史路況信息服務(wù)。
搜索類的服務(wù)接口需要指定一個(gè)搜索范圍,否則接口將不能工作。

本地搜索
  BMap.LocalSearch提供本地搜索服務(wù),在使用本地搜索時(shí)需要為其設(shè)置一個(gè)檢索區(qū)域,檢索區(qū)域可以是BMap.Map對(duì)象、 BMap.Point對(duì)象或者是省市名稱(比如:"北京市")的字符串。BMap.LocalSearch構(gòu)造函數(shù)的第二個(gè)參數(shù)是可選的,您可以在其中指 定結(jié)果的呈現(xiàn)。BMap.RenderOptions類提供了若干控制呈現(xiàn)的屬性,其中map指定了結(jié)果所展現(xiàn)的地圖實(shí)例,panel指定了結(jié)果列表的容 器元素。

復(fù)制代碼 代碼如下:

var map = new BMap.Map("container");     
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);     
var local = new BMap.LocalSearch(map, {     
      renderOptions:{map: map}     
});     
local.search("天安門");

  另外,BMap.LocalSearch還提包含searchNearby和searchInBounds方法,為您提供周邊搜索和范圍搜索服務(wù)。

配置搜索

  BMap.LocalSearch提供了若干配置方法,通過(guò)它們可以自定義搜索服務(wù)的行為以滿足您的需求。 在下面的示例中,我們調(diào)整每頁(yè)顯示8個(gè)結(jié)果,并且根據(jù)結(jié)果點(diǎn)位置自動(dòng)調(diào)整地圖視野,不顯示第一條結(jié)果的信息窗口:

復(fù)制代碼 代碼如下:

var map = new BMap.Map("container");   
map.centerAndZoom(new BMap.Point(116.404, 39.915), 14); 
var local = new BMap.LocalSearch("北京市",  
            {renderOptions: {map: map,autoViewport: true},pageCapacity: 8});     
local.search("中關(guān)村");
 
結(jié)果面板

  通過(guò)設(shè)置BMap.LocalSearchOptions.renderOptions.panel屬性,可以為本地搜索對(duì)象提供一個(gè)結(jié)果列表容器,搜索結(jié)果會(huì)自動(dòng)添加到容器元素中。請(qǐng)看下面示例:

復(fù)制代碼 代碼如下:

var map = new BMap.Map("container");    
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); 
var local = new BMap.LocalSearch(map,  
            {renderOptions: {map: map,panel: "results"});     
local.search("中關(guān)村");

數(shù)據(jù)接口

  除了搜索結(jié)果會(huì)自動(dòng)添加到地圖和列表外,您還可以通過(guò)數(shù)據(jù)接口獲得詳細(xì)的數(shù)據(jù)信息,結(jié)合地圖API您可以自行向地圖添加標(biāo)注和信息窗口。 BMap.LocalSearch和BMap.LocalSearchOptions類提供了若干設(shè)置回調(diào)函數(shù)的接口,通過(guò)它們可得到搜索結(jié)果的數(shù)據(jù)信 息。 例如,通過(guò)onSearchComplete回調(diào)函數(shù)參數(shù)可以獲得BMap.LocalResult對(duì)象實(shí)例,它包含了每一次搜索結(jié)果的數(shù)據(jù)信息。 當(dāng)回調(diào)函數(shù)被執(zhí)行時(shí),您可以使用BMap.LocalSearch.getStatus()方法來(lái)確認(rèn)搜索是否成功或者得到錯(cuò)誤的詳細(xì)信息。

在下面這個(gè)示例中,通過(guò)onSearchComplete回調(diào)函數(shù)得到第一頁(yè)每條結(jié)果的標(biāo)題和地址信息,并輸出到頁(yè)面上:

復(fù)制代碼 代碼如下:

var map = new BMap.Map("container");         
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);     
var options = {     
      onSearchComplete: function(results){     
          if (local.getStatus() == BMAP_STATUS_SUCCESS){     
                // 判斷狀態(tài)是否正確     
                var s = [];     
                for (var i = 0; i < results.getCurrentNumPois(); i ++){     
                    s.push(results.getPoi(i).title + ", " + results.getPoi(i).address);     
                }     
             document.getElementById("log").innerHTML = s.join("<br>");     
          }     
      }     
 };     
var local = new BMap.LocalSearch(map, options);     
local.search("公園");

周邊搜索

  通過(guò)周邊搜索服務(wù),您可以在某個(gè)地點(diǎn)附近進(jìn)行搜索,也可以在某一個(gè)特定結(jié)果點(diǎn)周圍進(jìn)行搜索。

  下面示例展示如何在前門附近搜索小吃:

復(fù)制代碼 代碼如下:

var map = new BMap.Map("container");        
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);     
var local = new BMap.LocalSearch(map,  
              { renderOptions:{map: map, autoViewport: true}});     
local.searchNearby("小吃", "前門");

矩形范圍搜索

  矩形范圍搜索將根據(jù)您提供的視野范圍提供搜索結(jié)果。注意:當(dāng)搜索范圍過(guò)大時(shí)可能會(huì)出現(xiàn)無(wú)結(jié)果的情況。

  下面示例展示在當(dāng)前地圖視野范圍內(nèi)搜索銀行:

復(fù)制代碼 代碼如下:

var map = new BMap.Map("container");       
map.centerAndZoom(new BMap.Point(116.404, 39.915), 14);     
var local = new BMap.LocalSearch(map,  
              { renderOptions:{map: map}});     
local.searchInBounds("銀行", map.getBounds());

矩形區(qū)域搜索例子

<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
 <style type="text/css">
  body, html {width: 100%;height: 100%;margin:0;font-family:"微軟雅黑";}
  #allmap{width:100%;height:500px;}
  p{margin-left:5px; font-size:14px;}
 </style>
 <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密鑰"></script>
 <title>搜索區(qū)域內(nèi)關(guān)鍵詞</title>
</head>
<body>
 <div id="allmap"></div>
 <p>返回北京市矩形框區(qū)域范圍內(nèi)的“銀行”關(guān)鍵字的檢索結(jié)果,并展示在地圖上</p>
</body>
</html>
<script type="text/javascript">
 // 百度地圖API功能
 var map = new BMap.Map("allmap");   // 創(chuàng)建Map實(shí)例
 map.centerAndZoom(new BMap.Point(116.274625,39.961627), 11);
 map.enableScrollWheelZoom();       //啟用滾輪放大縮小
  var local = new BMap.LocalSearch(map, {
  renderOptions:{map: map}
 });
 var pStart = new BMap.Point(116.274625,39.961627);
 var pEnd = new BMap.Point(116.367474,39.988609);
 var bs = new BMap.Bounds(pStart,pEnd); //自己規(guī)定范圍
 local.searchInBounds("銀行", bs);
  var polygon = new BMap.Polygon([
  new BMap.Point(pStart.lng,pStart.lat),
  new BMap.Point(pEnd.lng,pStart.lat),
  new BMap.Point(pEnd.lng,pEnd.lat),
  new BMap.Point(pStart.lng,pEnd.lat)
  ], {strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});
 map.addOverlay(polygon);
</script>

效果圖:

以上就是本文的全部?jī)?nèi)容,希望大家喜歡。

相關(guān)文章

最新評(píng)論