2017-07-13 9 views
0

私はちょっとしたJavascriptで10時間立ち往生しています。私は非常に若い開発者です。 localStorage内にいくつかの場所(JSON)を用意しました。特定の値(たとえばcategory = 2)のすべてのデータを選択してから、緯度と経度の最も近い場所を取得したいと考えています。localStorageのJSONと私のジオロケーションから最も近い場所を取得

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": "[email protected]", 
 
      "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": "[email protected]", 
 
      "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": "[email protected]", 
 
      "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

ちょうど私があなたのコードスニペットを実行しようとすると、私はスクリプトエラーを持つ配列

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": "[email protected]", 
 
      "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": "[email protected]", 
 
      "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": "[email protected]", 
 
      "site": "www.lechienelegant.be", 
 
      "categorie": "12", 
 
      "creation": "", 
 
      "latitude": "", 
 
      "longitude": "" 
 
     } 
 
    ] 
 
} 
 

 
for (place in jsonString.places) { 
 
\t if (jsonString.places[place].categorie == "12") { 
 
    \t console.log(jsonString.places[place].latitude, jsonString.places[place].longitude) 
 
\t } 
 
}

+0

を反復。 –

+0

ランコードスニペットをクリックすると、フィールドが空であるため何も表示されません –

+0

ありがとうございます。これらの結果をジオロケーションによってソートするのはどうですか? 私はこのリンクを見つけましたが、私の価値とは機能しません:http://jsfiddle.net/mitchmalone/Wf6qy/1/ –

関連する問題