Leaflet.TrackPlayBack icon indicating copy to clipboard operation
Leaflet.TrackPlayBack copied to clipboard

getCurrLatLng or getCurrentData

Open silverbyte opened this issue 6 years ago • 4 comments

getCurTime is great to get the current time, it would be even greater if it can return back current latlng along with Info. Example getCurrentData { lat: lng: Info: }. Maybe this would be heavy for multiple tracks but for single track would be good

silverbyte avatar May 25 '19 23:05 silverbyte

Really really needed! If I'm console-logging the objects, I can see there's arrays inside having all this info, but I'm not able to get this out currently.

answerquest avatar Oct 10 '19 15:10 answerquest

@silverbyte I found a way to get it! Take a look:

trackplayback.on('tick', e => {
    $('.info-cur-time').append(` ( ${parseInt(trackplayback.getCurTime())} )`);
    // console.log(trackplayback);
    // console.log(trackplayback.clock._trackController._draw._bufferTracks[0]); // this seems to be all the preceding points
    var bufferTracks = trackplayback.clock._trackController._draw._bufferTracks[0];
    // this json array holds all the points travelled uptill now with lat-longs, and if this is an interpolation then the interpolated point at the end.
    var latestPoint = bufferTracks[bufferTracks.length - 1];
    /* looks like: {"lat":12.9711727,"lng":80.2438891,"time":1564745951,"info":[{"key":"user","value":"test3"},..],"isOrigin":true} */

    if(! latestPoint['isOrigin'] && bufferTracks.length>1) {
    // if the last element in the array is an interpolated point, then take the second-last element which will hold the last reported GPS location.
    latestPoint = bufferTracks[bufferTracks.length - 2];
    }

    console.log(latestPoint);
    $('#currentLocation').html(`${parseFloat(latestPoint['lat']).toFixed(5)},${parseFloat(latestPoint['lng']).toFixed(5)}`);
    
    //map.setView(latestPoint); // for making the map follow the track. But danger : this is preventing rendering of the boat icon for some reason. Uncomment only if you're ok with losing that icon.
}, this);

answerquest avatar Oct 11 '19 03:10 answerquest

When we do console.log(trackplayback), a whole big complex object comes in browser console. I rummaged through it all, tried and failed at many places, finally got some headway in .clock._trackController._draw._bufferTracks[0]

answerquest avatar Oct 11 '19 11:10 answerquest

If you want to use public method you can do it by calling getTrackPointsBeforeTime from tracks

trackplayback.on('tick', e => {
        const rendered = trackplayback.tracks[0].getTrackPointsBeforeTime(e.time)
        
        if (rendered.length > 1) {
                const lastRendered = rendered[rendered.length - 2]
                console.log(lastRendered.info)
       }     
}, this)

drawback from above code is the method executed 2 times

raffifu avatar Jul 23 '21 15:07 raffifu