manuelbieh / geolib

Zero dependency library to provide some basic geo functions
MIT License
4.21k stars 341 forks source link

Calculated distance, then show and order #30

Closed renege closed 10 years ago

renege commented 10 years ago

Why isn't the following working:

<input type="number" id="distance" value="10">
<input type="text" id="userLatitude" value="52.880709">
<input type="text" id="userLongitude" value="5.4560155">
<button id="search">go</button>
$(document).on('click', '#search', function(){

    var userLatitude    = $('#userLatitude').val();
    var userLongitude   = $('#userLongitude').val();
    var user = { latitude: userLatitude, longitude: userLongitude };

    var distance = parseInt($('#distance').val()) * 100;

    console.log(distance);
 $('.user').hide();
    $('.user').each(function(index, value) { 
        var talent = $(this).data('talent');

        var lat = $(this).data('latitude');
        var lng = $(this).data('longitude');

        var current = { latitude: lat, longitude: lng };

        var calculated = geolib.isPointInCircle(
            current, 
            user,
            distance
        );  

        if(calculated){
            $(this).show();
        }

        console.log(calculated);

    });

    return false;

});

How to order this all by Distance? So the closest one shows first.

manuelbieh commented 10 years ago

There are two methods: geolib.orderByDistance() and geolib.findNearest()

They will probably do exactly what you're trying to do.