//加载门店 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根据两个地点的位置的经纬度获取距离
//根据俩地经纬度计算距离 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)); }
时间: 2024-10-23 20:48:07