【发布时间】:2014-09-16 14:02:46
【问题描述】:
我正在尝试查找附近的地方并计算从当前位置到附近位置的距离,然后我想在表格中显示所有地方,从当前位置的距离按递增顺序排序。现在我已经成功了
- 查找附近地点
- 查找当前地点到附近每个地点的距离
- 按照从谷歌收到的响应顺序显示每个这样的地点和距离 我已将数据添加为 json 数组并将其添加到控制台
我想对这个数组进行排序,显示排序后的数组,而不是步骤 3 中的直接结果。有人可以帮我吗?
DEMO
jQuery
>用于计算距离并附加到带有 id 显示的表格的代码
function createMarker(obj) {
// prepare new Marker object
var mark = new google.maps.Marker({
position: obj.geometry.location,
map: map,
title: obj.name
});
alert(obj.name);
var lat=document.getElementById('lat').value;
var long=document.getElementById('lng').value;
// alert(lat);
//alert(long);
var p1 = new google.maps.LatLng(lat,long);
var p2 = new google.maps.LatLng(obj.geometry.location.lat(),obj.geometry.location.lng());
google.maps.LatLng.prototype.distanceFrom = function(latlng) {
var lat = [this.lat(), latlng.lat()]
var lng = [this.lng(), latlng.lng()]
var R = 6378137;
var dLat = (lat[1]-lat[0]) * Math.PI / 180;
var dLng = (lng[1]-lng[0]) * Math.PI / 180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat[0] * Math.PI / 180 ) * Math.cos(lat[1] * Math.PI / 180 ) *
Math.sin(dLng/2) * Math.sin(dLng/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return Math.round(d);
}
// var loc1 = new GLatLng(lat, long);
//var loc2 = new GLatLng(obj.geometry.location.lat(), obj.geometry.location.lng());
var dist = p2.distanceFrom(p1);
//alert(dist/1000+'m');
var distn=dist/1000;
var dost=distn+(distn <1 ? "m" : "km")
$('#Display').append('<tr><td>'+obj.name+'</td><td>'+dost+'</td></tr>');
datas.push({ app:obj.name,message:distn});
//alert(google.maps.geometry.spherical.computeDistanceBetween(p1, p2)+'sa');
markers.push(mark);
// prepare info window
var infowindow = new google.maps.InfoWindow({
content: '<img src="' + obj.icon + '" /><font style="color:#000;">' + obj.name +
'<br />Rating: ' + obj.rating + '<br />Vicinity: ' + obj.vicinity + '</font>'
});
// add event handler to current marker
google.maps.event.addListener(mark, 'click', function() {
clearInfos();
infowindow.open(map,mark);
});
infos.push(infowindow);
console.log(datas);
// parse(datas);
}
JSON DATA 输出(样本)位置可能不同,但距离是字段
[{
"place": "Taïm",
"Distance": 1.193
}, {
"place": "Balthazar",
"Distance": 1.127
}, {
"place": "Tribeca Grill",
"Distance": 0.678
}, {
"place": "Lombardi's Pizza",
"Distance": 1.179
}, {
"place": "Whole Foods Market",
"Distance": 0.484
}, {
"place": "Best Western Bowery Hanbee Hotel",
"Distance": 1.015
}, {
"place": "Les Halles",
"Distance": 0.536
}, {
"place": "Cafe Gitane",
"Distance": 1.367
}, {
"place": "Shake Shack",
"Distance": 0.782
}, {
"place": "Bubby's",
"Distance": 0.64
}, {
"place": "Ninja New York",
"Distance": 0.397
}, {
"place": "La Esquina",
"Distance": 1.056
}, {
"place": "Peasant",
"Distance": 1.277
}, {
"place": "Apotheke",
"Distance": 0.658
}, {
"place": "Joe's Shanghai",
"Distance": 0.697
}, {
"place": "Kaffe 1668",
"Distance": 0.436
}, {
"place": "Rice To Riches",
"Distance": 1.199
}, {
"place": "SUBWAY® Restaurants",
"Distance": 0.382
}, {
"place": "Bouley",
"Distance": 0.384
}, {
"place": "North End Grill",
"Distance": 0.808
}]
【问题讨论】:
-
@geocodezip 我不想在将数据放入表后进行排序,我想在以 json 的形式之前进行排序
-
Array.sort() 也应该适用于您的 JSON 数组...
-
已经尝试过那个兄弟,但是你能帮我解决这个问题吗???
-
它应该可以工作。你尝试了什么?
-
@geocodezip 添加了解析数据和排序的代码
标签: javascript jquery json google-maps-api-3 google-places-api