3

I have calculated a destination from bearing/distance (feet) using cheap-ruler.js https://github.com/mapbox/cheap-ruler

Console.log() returns this array from the calculation..

[-87.36328091530362, 37.78808138412046]

I then am trying to add the result to the map using...

    var ruler = cheapRuler(35.2, 'feet');

    var bearing = document.getElementById('bearing').value;
    var distance = document.getElementById('distance').value;

    var calc = ruler.destination(lnglat, 0.1, 90);

    destlnglat = [calc[0], calc[1]];

    var myIcon = L.divIcon({className: 'css-icon'});

    // Pass array to layer...
    var destinationPoint = L.geoJson(destlnglat, {
    pointToLayer: function (feature, destlnglat) {
            return new L.Marker(destlnglat, {icon: myIcon });
        }
    }).addTo(map);

Which returns no error in the console? How do I correctly pass back to L.geoJson()?

Edit: I got this working using the following..

function calculateDestinationFt() {

    var ruler = cheapRuler(35.2, 'feet');

    var bearing = document.getElementById('bearing').value;
    var distance = document.getElementById('distance').value;

    var calc = ruler.destination(lnglat, bearing, distance);

    var myIcon = L.divIcon({className: 'css-icon'});

    var m_coords = {
        lng: calc[0],
        lat: calc[1]
    };

    var destination = {
                        "type": "FeatureCollection",
                        "features": [{
                            "type": "Feature",
                            "properties": {

                            },
                            "geometry": {
                                "type": "Point",
                                "coordinates": [m_coords.lng,m_coords.lat]
                            }
                        }]
                        }   

    var destinationPoint = L.geoJson(destination, {
    pointToLayer: function (feature, lnglat) {
            return new L.Marker(lnglat, {icon: myIcon });
        }
    }).addTo(map);
}

But my results are waaaay off? I got these results pluging in a bearing:270 and distance 5280ft? Neither ft or bearing appear correct. AaAH!!

enter image description here

2

Simple answer!

You're got your bearing and distance around the wrong way. From the docs: https://github.com/mapbox/cheap-ruler#destinationp-dist-bearing

Quote:

destination(p, dist, bearing)

Returns a new point given distance and bearing from the starting point.

var point = ruler.destination([30.5, 50.5], 0.1, 90);
  • Haha. Omg. Thanks for pointing this out @Alex Leith. Im surprised it provided any solution at all? Here's what works... var calc = ruler.destination(lnglat, distance, bearing ); – wtgeographer Mar 5 '17 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.