socib / Leaflet.TimeDimension

Add time dimension capabilities on a Leaflet map.
MIT License
435 stars 139 forks source link

How to get the timestamp for the given slider event so that I can show the weather? #171

Open maddan opened 5 years ago

maddan commented 5 years ago

Hi, I'm using Leaflet framework. For weather data, I'm using Open Weather Map api. However, for the slider I'm using nowCoast (NOAA). I'm using the Leaflet.TimeDimension and while dragging the slider, I have to update the marker metadata in my map. How do I do that? Did I miss any important documentation? Please help.

Here's my Leaflet map instance:

var map = L.map('map', {
                layers: [osmMap], // only add one!
                fullscreenControl: true,    // Fullscreen map
                timeDimension: true,
                timeDimensionControl: true,
                timeDimensionOptions:{
                    //timeInterval: "PT4H/" + endDate.toISOString(),
                    timeInterval: "2019-05-16/2019-05-20",
                    period: "PT4M",
                    currentTime: endDate
                },    

                timeDimensionControlOptions: {
                    autoPlay: false,
                    playerOptions: {
                        buffer: 10,
                        transitionTime: 250,
                        loop: true,
                    }
                },
            })
            .setView([latVal, lngVal], 10);

Based on where the user slides, I need to update my marker (weather data) as I slide the slider given that timestamp.

DrPDash commented 5 years ago

If I understood your question correctly, you need to extract the exact time on the display bar?

It's a simple need, intuitively, but made me ponder over it for a few days. The timeDimension is super attractive but anything slightly out of the ordinary comes with a price.

Anyways, try the following: (1) use the moment js lib for formatting purposes You may find on CDN or drop me an email cdnjs.cloudflare.com/ajax/libs/moment.js/2.7.0/moment.min.js

(2) extract/format the date as suited to your needs: map.timeDimension.on('timeload', function (data) { var nowTime = map.timeDimension.getCurrentTime(); var tzoffset = (new Date()).getTimezoneOffset() * 60000; var localISOTime = (new Date(new Date(nowTime) - tzoffset)).toISOString().slice(0, -1); _this_YYYY_MM_DD_stamp = moment(localISOTime).format(returnDateFormat_YYYY_MM_DD_dataFetch);}, this);

BTW, I am also conceptualizing an Ocean Viewer for NOAA.

Hope this helps.

maddan commented 5 years ago

You are right! It looks great to use it but really hard to customize it. Let me try your suggestion. Thanks!