Chinaunix首页 | 论坛 | 博客
  • 博客访问: 113792
  • 博文数量: 40
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 423
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-15 11:55
文章分类

全部博文(40)

文章存档

2016年(36)

2015年(2)

2013年(2)

我的朋友

分类: jQuery

2016-04-27 17:05:02

在开发项目中遇到调取百度地图周边设施,总结部分代码
html部分

点击(此处)折叠或打开

  1. <a href="map.html">
  2.                 <div id="l-map" style="width:100%;height:256px;"></div>
  3.             </a>
  4.             <ul class="map-tab mt20">
  5.                 <li class="map-tab1"> <i></i>
  6.                     交通
  7.                 </li>
  8.                 <li class="map-tab2">
  9.                     <i></i>
  10.                     商业
  11.                 </li>
  12.                 <li class="map-tab3">
  13.                     <i></i>
  14.                     教育
  15.                 </li>
  16.                 <li class="map-tab4">
  17.                     <i></i>
  18.                     医院
  19.                 </li>
  20.                 <li class="map-tab5">
  21.                     <i></i>
  22.                     附近楼盘
  23.                 </li>
  24.             </ul>
  25.             <div class="clear"></div>
  26.             <ul class="map-item map-item1"></ul>
  27.             <ul class="map-item map-item2"></ul>
  28.             <ul class="map-item map-item3"></ul>
  29.             <ul class="map-item map-item4"></ul>
  30.             <ul class="map-item map-item5"></ul>
  31.             <div class="more">查看更多</div>


js部分

点击(此处)折叠或打开

  1. $(function(){
  2.      map();
  3.     });

  4.     var map;
  5.     var my_radius = 1000; //默认周边圆的单位()

  6.     var point = new BMap.Point(<%= @item[:local_x] %>,<%= @item[:local_y] %>);
  7.     var Search = {
  8.      markersInfo: [],
  9.      metaMarkers: []
  10.     };

  11.   function map() {
  12.     // 百度地图API
  13.     map = new BMap.Map("l-map", {
  14.       minZoom: 15
  15.     }); //初始化地图,规定最小缩放
  16.     map.centerAndZoom(point, 16); //显示中心
  17.     //map.enableScrollWheelZoom(); //启用滚轮缩放
  18.     map.disableDragging();
  19.     var marker = new BMap.Marker(point); // 创建标注
  20.     map.addOverlay(marker); // 将标注添加到地图中

  21.     var label = new BMap.Label("<%= @item[:item_name] %>", {
  22.       offset: new BMap.Size(20, -10)
  23.     });
  24.     marker.setLabel(label);
  25.     LocationSearch("公交");
  26.   };
  27.     
  28.   function LocationSearch(key) {
  29.     searchNearbys(point, key, my_radius); //执行搜索
  30.   };

  31.   function searchNearbys(point, key, radius) {
  32.     var local = new BMap.LocalSearch(point, {
  33.       onSearchComplete: searchComplete
  34.     }); //构造一个查询
  35.     local.searchNearby(
  36.       key,
  37.       point,
  38.       radius
  39.     );
  40.   }

  41.   function searchComplete(results) {
  42.     searchCompletes(results);
  43.   }

  44.   function searchCompletes(results) {
  45.     var index = 1; //从1开始
  46.     var s = [];
  47.     var info = null;
  48.     var num = results.getCurrentNumPois();
  49.     Search.markersInfo = []; //制空
  50.     Search.metaMarkers = [];
  51.     for (var i = 0; i < num; i++) {
  52.       var result = results.getPoi(i);
  53.       var dis = returnFloat1(parseInt(map.getDistance(point, result.point)));
  54.       Search.markersInfo.push({
  55.         'lng': result.point.lng,
  56.         'lat': result.point.lat,
  57.         'subject': result.title,
  58.         'address': result.address,
  59.         'dis': dis
  60.       });
  61.     }
  62.     //遍历结果
  63.      s.push('
      ')
    •       for (var j = 0; j < Search.markersInfo.length; j++) {
    •         var result = Search.markersInfo[j];
    •         var display = j>2 ? "hide":"show";
    •          s.push('
    • +display+'">'+result.subject +'距离:'+result.dis +'米
    • ');
    •         index++;
    •       }
    •      s.push('
    '
    );
  64.       panelShow(s);

  65.   }

  66.   function panelShow(s) {
  67.     var html = '';
  68.     if (s) {
  69.       html = s.join("");
  70.     }
  71.     $(".map-item").html(html);
  72.   }

  73.   //距离
  74.   function returnFloat1(value) { //保留一位小数点
  75.     value = Math.round(parseFloat(value) * 10) / 10;
  76.     if (value.toString().indexOf(".") < 0)
  77.       value = value.toString() + "";
  78.     return value;
  79.   }

  80.     /******地图tab*******/
  81.     $(".map-tab li").on('click',function(){
  82.         $(".map-tab li").css('background','#fff');
  83.         $(this).css('background','#f4f4f4');
  84.         $(".more").show();
  85.         var tabClass = $(this).attr("class");
  86.         switch(tabClass){
  87.          case "map-tab1":
  88.          LocationSearch("交通");
  89.          break;
  90.          case "map-tab2":
  91.          LocationSearch("商业");
  92.          break;
  93.          case "map-tab3":
  94.          LocationSearch("教育");
  95.          break;
  96.          case "map-tab4":
  97.          LocationSearch("医院");
  98.          break;
  99.          case "map-tab5":
  100.          LocationSearch("附近楼盘");
  101.          break;
  102.         }
  103.         //
  104.     });
  105.     $(".more").on('click',function(){
  106.      $(".map-item li").show();
  107.      $(this).hide();
  108.     })


只要通过后台调取经纬度就可以了
阅读(1327) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~