title: JSON longitude latitude nearest location
body: <p>I am making a script, I have about 2000 city adresses in my json file with longitutde and latitude. </p>
  
  <p>The script has to calculate with longitude and latitude the distance from the supplied lat/long to each of the entries and </p>
  
  <p>order them in order of nearest. </p>
  
  <p>I need about 10 "nearest" results. </p>
  
  <p>I can calculate the distance, but I can't sort by nearest location. </p>
  
  <p><strong><a href="http://jsfiddle.net/Wf6qy/" rel="nofollow">http://jsfiddle.net/Wf6qy/</a></strong></p>
  
  <p>JSON File : </p>
  
  <pre><code>{
      "City A": {
          "Position": {
              "Longitude": 9.96233,
              "Latitude": 49.80404
          }
      },
      "City B": {
          "Position": {
              "Longitude": 6.11499,
              "Latitude": 50.76891
          }
      },
      "City C": {
          "Position": {
              "Longitude": 6.80592,
              "Latitude": 51.53548
          }
      },
      "City D": {
          "Position": {
              "Longitude": 9.50523,
              "Latitude": 51.31991
          }
      },
      "City E": {
          "Position": {
              "Longitude": 9.66089,
              "Latitude": 48.70158
          }
      },
      "City F": {
          "Position": {
              "Longitude": 9.93368,
              "Latitude": 53.55608
          }
      },
      "City G": {
          "Position": {
              "Longitude": 11.56122,
              "Latitude": 48.14496
          }
      },
      "City H": {
          "Position": {
              "Longitude": 13.34253,
              "Latitude": 52.5319
          }
      },
      "City I": {
          "Position": {
              "Longitude": 6.11327,
              "Latitude": 50.77715
          }
      },
      "City J": {
          "Position": {
              "Longitude": 13.36671,
              "Latitude": 52.54344
          }
      }
  }
  </code></pre>
  
  <p>jQuery : </p>
  
  <pre><code>var jsonString =
      '{"City A":{"Position":{"Longitude":9.96233,"Latitude":49.80404}},"City B":{"Position":{"Longitude":6.11499,"Latitude":50.76891}},"City C":{"Position":{"Longitude":6.80592,"Latitude":51.53548}},"City D":{"Position":{"Longitude":9.50523,"Latitude":51.31991}},"City E":{"Position":{"Longitude":9.66089,"Latitude":48.70158}},"City F":{"Position":{"Longitude":9.93368,"Latitude":53.55608}},"City G":{"Position":{"Longitude":11.56122,"Latitude":48.14496}},"City H":{"Position":{"Longitude":13.34253,"Latitude":52.5319}},"City I":{"Position":{"Longitude":6.11327,"Latitude":50.77715}},"City J":{"Position":{"Longitude":13.36671,"Latitude":52.54344}}}';
  
  var myData = JSON.parse(jsonString);
  
  $(document).ready(function () {
  
      $.each(myData, function (a, b) {
  
          //$("#groups").append("&lt;li&gt;"+b.Position.Longitude+"&lt;/li&gt;");
  
          $("#groups").append('&lt;li&gt;&lt;h2&gt;' + a + '&lt;/h2&gt;&lt;p&gt;' + hesapla(9.9608999, 49.7222842, b.Position.Longitude, b.Position.Latitude) + '&lt;/p&gt;&lt;/a&gt;&lt;/li&gt;');
  
      });
  
  
  
  
      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));
  
          if (d &lt; 1) {
              return Math.round(d * 1000) +" m"
              ;
          } else {
              return Math.round(d * 10) / 10 +" km"
              ;
          }
      };
  
  
  
  });
  </code></pre>