在开发项目中遇到调取百度地图周边设施,总结部分代码
html部分
-
<a href="map.html">
-
<div id="l-map" style="width:100%;height:256px;"></div>
-
</a>
-
<ul class="map-tab mt20">
-
<li class="map-tab1"> <i></i>
-
交通
-
</li>
-
<li class="map-tab2">
-
<i></i>
-
商业
-
</li>
-
<li class="map-tab3">
-
<i></i>
-
教育
-
</li>
-
<li class="map-tab4">
-
<i></i>
-
医院
-
</li>
-
<li class="map-tab5">
-
<i></i>
-
附近楼盘
-
</li>
-
</ul>
-
<div class="clear"></div>
-
<ul class="map-item map-item1"></ul>
-
<ul class="map-item map-item2"></ul>
-
<ul class="map-item map-item3"></ul>
-
<ul class="map-item map-item4"></ul>
-
<ul class="map-item map-item5"></ul>
-
<div class="more">查看更多</div>
js部分
-
$(function(){
-
map();
-
});
-
-
var map;
-
var my_radius = 1000; //默认周边圆的单位(米)
-
-
var point = new BMap.Point(<%= @item[:local_x] %>,<%= @item[:local_y] %>);
-
var Search = {
-
markersInfo: [],
-
metaMarkers: []
-
};
-
-
function map() {
-
// 百度地图API
-
map = new BMap.Map("l-map", {
-
minZoom: 15
-
}); //初始化地图,规定最小缩放
-
map.centerAndZoom(point, 16); //显示中心
-
//map.enableScrollWheelZoom(); //启用滚轮缩放
-
map.disableDragging();
-
var marker = new BMap.Marker(point); // 创建标注
-
map.addOverlay(marker); // 将标注添加到地图中
-
-
var label = new BMap.Label("<%= @item[:item_name] %>", {
-
offset: new BMap.Size(20, -10)
-
});
-
marker.setLabel(label);
-
LocationSearch("公交");
-
};
-
-
function LocationSearch(key) {
-
searchNearbys(point, key, my_radius); //执行搜索
-
};
-
-
function searchNearbys(point, key, radius) {
-
var local = new BMap.LocalSearch(point, {
-
onSearchComplete: searchComplete
-
}); //构造一个查询
-
local.searchNearby(
-
key,
-
point,
-
radius
-
);
-
}
-
-
function searchComplete(results) {
-
searchCompletes(results);
-
}
-
-
function searchCompletes(results) {
-
var index = 1; //从1开始
-
var s = [];
-
var info = null;
-
var num = results.getCurrentNumPois();
-
Search.markersInfo = []; //制空
-
Search.metaMarkers = [];
-
for (var i = 0; i < num; i++) {
-
var result = results.getPoi(i);
-
var dis = returnFloat1(parseInt(map.getDistance(point, result.point)));
-
Search.markersInfo.push({
-
'lng': result.point.lng,
-
'lat': result.point.lat,
-
'subject': result.title,
-
'address': result.address,
-
'dis': dis
-
});
-
}
-
//遍历结果
-
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('');
-
panelShow(s);
-
-
}
-
-
function panelShow(s) {
-
var html = '';
-
if (s) {
-
html = s.join("");
-
}
-
$(".map-item").html(html);
-
}
-
-
//距离
-
function returnFloat1(value) { //保留一位小数点
-
value = Math.round(parseFloat(value) * 10) / 10;
-
if (value.toString().indexOf(".") < 0)
-
value = value.toString() + "";
-
return value;
-
}
-
-
/******地图tab*******/
-
$(".map-tab li").on('click',function(){
-
$(".map-tab li").css('background','#fff');
-
$(this).css('background','#f4f4f4');
-
$(".more").show();
-
var tabClass = $(this).attr("class");
-
switch(tabClass){
-
case "map-tab1":
-
LocationSearch("交通");
-
break;
-
case "map-tab2":
-
LocationSearch("商业");
-
break;
-
case "map-tab3":
-
LocationSearch("教育");
-
break;
-
case "map-tab4":
-
LocationSearch("医院");
-
break;
-
case "map-tab5":
-
LocationSearch("附近楼盘");
-
break;
-
}
-
//
-
});
-
$(".more").on('click',function(){
-
$(".map-item li").show();
-
$(this).hide();
-
})
只要通过后台调取经纬度就可以了
阅读(1327) | 评论(0) | 转发(0) |