百度地图实现自动定位,周围目标检索,路线规划及面板导航功能(只贴出了JS)

首先得引入你的AK这里就不说了

var url = decodeURI(window.location.href);
//var url=window.location.href;
var data = url.split("?")[1];
var datas = data.split(",");
$("#h3").html(datas[3]);

var map = new BMap.Map("l-map");

var lng, lat;
var geolocation = new BMap.Geolocation();
var geoc = new BMap.Geocoder();
//------------------ 
// 开启SDK辅助定位
geolocation.enableSDKLocation();
geolocation
.getCurrentPosition(function(r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS) {


//map.panTo(r.point);
lng = r.point.lng;
lat = r.point.lat;
//  alert('您的位置:'+r.point.lng+','+r.point.lat);
var mpoint = new BMap.Point(r.point.lng, r.point.lat);
map.centerAndZoom(mpoint, 14);
//缩放控件
map.addControl(new BMap.NavigationControl());
//比例尺控件
map.addControl(new BMap.ScaleControl());
//定位控件
map.addControl(new BMap.GeolocationControl());
var mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.addControl(new BMap.MapTypeControl()); //添加地图类型控件
map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
map.enableDragging(); //标记开启拖拽 
//--------------------------------------



//--------------------------------------

//解析地址,并规划路线
//根据经纬度
 geoc.getPoint(
         datas[3]+datas[0],
function(point) {
         if(point){
                       var p2 = new BMap.Point(point.lng,point.lat);
var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map,panel : "r-result", autoViewport: true,enableDragging : true }});
                       //driving.search(mpoint, p2);
                       
                       geoc.getLocation(mpoint, function(rs) {
var addComp = rs.addressComponents;
addrss = addComp.province + addComp.city
+ addComp.district + addComp.street
+ addComp.streetNumber;
driving.search({title: addrss, point: mpoint}, {title: datas[3], point: p2});
                       });
         }else{
         alert("地址无法找到");
         }
         },"北京市")  
                         //根据地址
/*  geoc.getLocation(mpoint, function(rs) {
var addComp = rs.addressComponents;
addrss = addComp.province + addComp.city
+ addComp.district + addComp.street
+ addComp.streetNumber;

//开车路线规划
var driving = new BMap.DrivingRoute(map, {
renderOptions : {
map : map,
panel : "r-result",
autoViewport : true
},
policy : "最短距离"
});
var stop = datas[3]+datas[0];
console.log(stop);
driving.search(addrss, stop);
});   */
//----------------------------------
//圆形区域检索指定数据
//var jl=[];
//var myGeo = new BMap.Geocoder();
var index = 0;
var list;
function bdGEO() {
var add = list[index];
geocodeSearch(add);


}
function geocodeSearch(add) {
if (index < list.length) {
//setTimeout(window.bdGEO,100);
index++;
if (index == list.length) {
return;
}
setTimeout(bdGEO(), 300);
}


geoc
.getPoint(
add.address,
function(point) {
//console.log("地址解析"+point.lng);
if (point) {
var latt = point.lat;
var lngg = point.lng;
var point2 = new BMap.Point(
lngg, latt); //终点  
var point1 = new BMap.Point(
lng, lat); //起点
var strjuli = map
.getDistance(
point1,
point2); //两地直接的直线距离  
if (strjuli <= 3000) {
 var marker = new BMap.Marker(
point2); // 创建标注
map.addOverlay(marker);
//标题
var opts1 = {
title : ''
+ add.name
+ '
'
};
//信息窗口
var infoWindow = new BMap.InfoWindow(
"
地址:  "
+ add.address
+ "
电话:  "
+ add.tel
+ "
",
opts1);  
//-----------------------------------

/*   var markergg = new BMap.Marker(point2);
   map.addOverlay(markergg); //添加GPS marker
   var labelgg = new BMap.Label("未转换的GPS坐标(错误)",{offset:new BMap.Size(20,-10)});
   markergg.setLabel(labelgg); //添加GPS label  */ 


   //转换了坐标反而定位不准了
/*  translateCallback = function (data){
     if(data.status === 0) {
      var marker = new BMap.Marker(
     data.points[0]); // 创建标注
map.addOverlay(marker);
//标题
var opts1 = {
title : ''
+ add.name
+ '
'
};
//信息窗口
var infoWindow = new BMap.InfoWindow(
"
地址:  "
+ add.address
+ "
电话:  "
+ add.tel
+ "
",
opts1);  
     
marker
.addEventListener(
"click",
function() {
this
.openInfoWindow(infoWindow); 

});  
      
     }
   }*/


  /*  
   var convertor = new BMap.Convertor();
   var pointArr = [];
       pointArr.push(point2);
   convertor.translate(pointArr, 1, 5, translateCallback) */
 

//-----------------------------------
//点击事件
  marker
.addEventListener(
"click",
function() {
this
.openInfoWindow(infoWindow); 

});  


//map.centerAndZoom(point2,12);


}
}
}, "北京市");


}
//查询起点附近刻制社
$("#search").click(function() {
$.ajax({
url : "weixin/factory/getFactoryListGq.do",
type : "post",
dataType : "json",
success : function(data) {

list = data.factoryNameList;


},
complete : function(testStust) {
bdGEO();
}
});


});





} else {
alert('failed' + this.getStatus());
}

});


百度地图实现自动定位,周围目标检索,路线规划及面板导航功能(只贴出了JS)_第1张图片

你可能感兴趣的:(百度地图开发)