百度地图插件开发使用三 及jquery function(a.b)排序等
//加载门店 function queryStoreList(id){ showLoad(); var search = $("#search").val(); var cityId = $("#cityId").val(); if(id !=null && id !=''){ cityId=id; } $.ajax({ url:ctx+"/weixin/store/queryStoreList", data:{ wechatId : ${param.wechatId}, search : search, cityId : cityId }, success:function(data){ hideLoad(); if(data != null && data.length > 0){ var html = ""; $.each(data,function(i,a){//计算距离
//$.each(data,function(i,a){ 【data对象也可以写在$(data).each(function(i,a){ 方法体 })】 })循环 var distance=getFlatternDistance(a.latitude,a.longitude,nowLat,nowLng);
//getFlatternDistance函数 根据两个地点之间的经纬度算出距离 distance=distance.toFixed(0); //保留小数点后面的位数 a.distance=distance; }) data=data.sort(function(a,b){//按照路程从小到大排序
//data.sort(function(a,b){ 【data.sort(参数序列) 】 默认是正序 }) return a.distance-b.distance; }); $.each(data,function(i,a){ html += "<div class='list_ce clearfix'>";
//class='list_ce' clearfix' 类选择器作为样式选择了,在CSS这两哥样式做了定义,clearFix有去除浮标的作用,防止模块位移 html += "<ul class='clearfix'>"; html += "<li class='one'><img src='${res}/upload/"+a.wechatId+"/"+a.image+"'></li>"; html += "<li class='two'>"; var dis=Number(a.distance); if(dis >1000){ dis=dis/1000 dis=dis+"千米"; }else{ dis=dis+"米"; } html += "<h2>"+a.name+ dis+"<span><a href='tel:"+a.phone+"'><img
src='${res}/template/002/images/m2.png'></a></span></h2>"; html += "<p class='fonts'>地址:"+a.address+" </p>"; html += "<p class='ft'>电话:<a href='tel:"+a.phone+"'>"+a.phone+"</a></p>"; html += "</li>"; html += "<li class='tre'>"; html += "<p onclick='goGps("+a.id+")'><img src='${res}/template/002/images/m1.png'></p>"; html += "</li>"; html += "</ul>"; html += "</div>"; html += "<div class='bres'></div>"; }) $("#menuList").empty().append(html);
//清空后重新赋值 }else{ } } }) }
函数:
getFlatternDistance根据两个地点的位置的经纬度获取距离
var nowLng;
var nowLat;
var EARTH_RADIUS = 6378137.0; //单位M
var PI = Math.PI;
function getRad(d){
return d*PI/180.0;
}
//根据俩地经纬度计算距离
function getFlatternDistance(lat1,lng1,lat2,lng2){
var f = getRad(parseFloat(lat1 + lat2)/2);
var g = getRad(parseFloat(lat1 - lat2)/2);
var l = getRad(parseFloat(lng1 - lng2)/2);
var sg = Math.sin(g);
var sl = Math.sin(l);
var sf = Math.sin(f);
var s,c,w,r,d,h1,h2;
var a = EARTH_RADIUS;
var fl = 1/298.257;
sg = sg*sg;
sl = sl*sl;
sf = sf*sf;
s = sg*(1-sl) + (1-sf)*sl;
c = (1-sg)*(1-sl) + sf*sl;
w = Math.atan(Math.sqrt(s/c));
r = Math.sqrt(s*c)/w;
d = 2*w*a;
h1 = (3*r -1)/2/c;
h2 = (3*r +1)/2/s;
return d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));
}
获取当前位置的经纬度
// 百度地图定位功能
function queryLocation(){
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445); //设置初始中心点
var geolocation = new BMap.Geolocation(); //获得当前位置的对象
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
var mk = new BMap.Marker(r.point);
nowLng=r.point.lng;
nowLat=r.point.lat;
}
else {
alert('failed'+this.getStatus());
}
},{enableHighAccuracy: true})
}