Search code examples
jqueryjson

JSON longitude latitude nearest location


I am making a script, I have about 2000 city adresses in my json file with longitutde and latitude.

The script has to calculate with longitude and latitude the distance from the supplied lat/long to each of the entries and

order them in order of nearest.

I need about 10 "nearest" results.

I can calculate the distance, but I can't sort by nearest location.

http://jsfiddle.net/Wf6qy/

JSON File :

{
    "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
        }
    }
}

jQuery :

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("<li>"+b.Position.Longitude+"</li>");

        $("#groups").append('<li><h2>' + a + '</h2><p>' + hesapla(9.9608999, 49.7222842, b.Position.Longitude, b.Position.Latitude) + '</p></a></li>');

    });




    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 < 1) {
            return Math.round(d * 1000) +" m"
            ;
        } else {
            return Math.round(d * 10) / 10 +" km"
            ;
        }
    };



});

Solution

  • My solution just creates an array of objects out of the locations and distances, sorts them, then spits them out. I've kept everything as meters instead of kilometers but it will be easy to tweak. Enjoy!

    While looping:

    distanceObj[i] = {
        distance: hesapla(9.9608999, 49.7222842, b.Position.Longitude, b.Position.Latitude),
        location: a
    };
    ++i;
    

    Then sort:

    distanceObj.sort(function(a,b) {
        return parseInt(a.distance) - parseInt(b.distance)
    });
    

    This will explain better: http://jsfiddle.net/mitchmalone/Wf6qy/1/