我在一个小Javascript上被困了10个小时。我是一个非常年轻的开发人员。 我在localStorage中存储了一些位置(JSON),我希望选择具有特定值的所有数据(例如“category = 2”),然后获取纬度和经度的最近位置。
var jsonString = "{
"version": "1.0.0",
"places": [
{
"id": "564",
"segment": "Europe (Avenue De L')",
"adresse": "Avenue De L'Europe 58",
"enseigne": "Eko Services",
"responsable": "",
"tel": "071\/30.70.02",
"email": "charleroi@ekoservices.be",
"site": "www.ekoservices.be",
"categorie": "12",
"creation": "",
"latitude": "",
"longitude": ""
},
{
"id": "565",
"segment": "Fort (Rue Du)",
"adresse": "Rue Du Fort 68",
"enseigne": "ALE Charleroi",
"responsable": "Mme. Francia Annick (Responsable)",
"tel": "071\/20.80.90",
"email": "ale.charleroi@skynet.be",
"site": "",
"categorie": "11",
"creation": "",
"latitude": "",
"longitude": ""
},
{
"id": "566",
"segment": "Beffroi (Rue Du)",
"adresse": "Rue du Beffroi 30",
"enseigne": "Le Chien Elegant",
"responsable": "Mme. Cilli Christine",
"tel": "071\/30.80.30",
"email": "lechienelegant@skynet.be",
"site": "www.lechienelegant.be",
"categorie": "12",
"creation": "",
"latitude": "",
"longitude": ""
}
]
} "
//var myData = JSON.parse(localStorage.getItem("placesJSON"));
$(document).ready(function () {
var distanceObj = [],
i = 0;
$.each(myData, function (a, b) {
distanceObj[i] = { distance: hesapla(9.9608999, 49.7222842, b.places.longitude, b.places.Latitude), location: a };
++i;
});
distanceObj.sort(function(a,b) {
return parseInt(a.distance) - parseInt(b.distance)
});
$.each(distanceObj, function(a, b) {
$('#places').append('<li>' + b.location + ': ' + b.distance + 'm</li>');
});
console.log(distanceObj);
function hesapla(meineLongitude, meineLatitude, long1, lat1) {
erdRadius = 6371;
meineLongitude = meineLongitude * (Math.PI / 180);
meineLatitude = meineLatitude * (Math.PI / 180);
long1 = long1 * (Math.PI / 180);
lat1 = lat1 * (Math.PI / 180);
x0 = meineLongitude * erdRadius * Math.cos(meineLatitude);
y0 = meineLatitude * erdRadius;
x1 = long1 * erdRadius * Math.cos(lat1);
y1 = lat1 * erdRadius;
dx = x0 - x1;
dy = y0 - y1;
d = Math.sqrt((dx * dx) + (dy * dy));
return Math.round(d * 1000);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="places"></div>
感谢您的帮助!
答案 0 :(得分:0)
只需遍历数组
x:Key