我在 jquery mobile 中有一个列表。我已经添加了代码来根据我所处的最近状态来处理列表。出于某种原因,我的逻辑不起作用。任何想法为什么?滚动时列表也非常缓慢。
检查此链接并单击左上角的查找最近的位置按钮
http://www.jm.bugs3.com/gl/state.html
<script>
function findMe(){
if (navigator.geolocation !=undefined){
navigator.geolocation.watchPosition(onFound, handleError);
}
}
function onFound(position){
var userLat = position.coords.latitude;
var userLong = position.coords.longitude;
$('ul li').each(function (index){
var locationLat = $(this).find('.lat').html();
var locationLong = $(this).find('.long').html();
var distance = getDistance(userLat, locationLat, userLong, locationLong);
$(this).data("distance", distance);
})
reOrder();
}
function handleError(error){
alert ("Could not find location");
}
function reOrder(){
$('ul li').sort(sortAlpha).appendTo('ul');
}
function sortAlpha(a, b){
return $(a).data('distance') > $(b).data('distance') ? 1 : -1; //if True or false 1 or -1
};
//Calculate the shortest distance based on lat and long
function getDistance(lat1, lat2, lon1, lon2){
var R = 6371; //KM
var d = Math.acos(Math.sin(lat1) * Math.sin(lat2) +
Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R
return d
};
// 这是带有纬度和经度坐标的列表的代码
<div data-role="content">
<ul data-role="listview" data-inset="true">
<li>
<a href="#page2" data-transition="slide" >Alabama</a>
<div class="lat" style="visibility:hidden">31.375626</div>
<div class="long" style="visibility:hidden">-86.299592</div>
</li>
<li>
<a href="#page3" data-transition="slide">Alaska</a>
<div class="lat" style="visibility:hidden">60.216736</div>
<div class="long" style="visibility:hidden">-149.882995</div>
</li>
<li>
<a href="#page4" data-transition="slide">Arizona</a>
<div class="lat" style="visibility:hidden">32.447659</div>
<div class="long" style="visibility:hidden">-112.134568</div>
</li>
<li>
<a href="#page5" data-transition="slide" >Arkansas</a>
<div class="lat" style="visibility:hidden">33.678252</div>
<div class="long" style="visibility:hidden">-92.340846</div>
</li>
<li>
<a href="#page6" data-transition="slide" >Boston</a>
<div class="lat" style="visibility:hidden">41.358431</div>
<div class="long" style="visibility:hidden">-71.059773</div>
</li>