perliedman / leaflet-realtime

Put realtime data on a Leaflet map
ISC License
743 stars 167 forks source link

l.realtime not a function problem #174

Closed Aaron040222 closed 4 years ago

Aaron040222 commented 4 years ago

I import the l.realtime through this in java script. var script = document.createElement('script'); script.type = "text/javascript"; script.async = true; script.src = 'https://cdnjs.cloudflare.com/ajax/libs/leaflet-realtime/2.0.0/leaflet-realtime.min.js'

And then I change the original geojson function to l.real time like this console.log(geoJson) var geoJsonLayers = L.Realtime(geoJson, { pointToLayer: (feature = {}, latlng) => { const { properties = {} } = feature; let updatedFormatted; let casesString;

    const {
      country,
      updated,
      cases,
      deaths,
      recovered
    } = properties

    casesString = `${cases}`;

    if ( cases > 1000 ) {
      casesString = `${casesString.slice(0, -3)}k+`
    }

    if ( updated ) {
      updatedFormatted = new Date(updated).toLocaleString();
    }

    const html = `
      <span class="icon-marker">
        <span class="icon-marker-tooltip">
          <h2>${country}</h2>
          <ul>
            <li><strong>Confirmed:</strong> ${cases}</li>
            <li><strong>Deaths:</strong> ${deaths}</li>
            <li><strong>Recovered:</strong> ${recovered}</li>
            <li><strong>Last Update:</strong> ${updatedFormatted}</li>
          </ul>
        </span>
        ${ casesString }
      </span>
    `;

    return L.marker( latlng, {
      icon: L.divIcon({
        className: 'icon',
        html
      }),
      riseOnHover: true
    });
  }
});
geoJsonLayers.addTo(map)

} However, it is showing that l.realtime is not a function. I am not sure how to fix it. Sorry it this is a stupid question, but I just start learning coding a few weeks before. Thank you!

perliedman commented 4 years ago

Hi!

Hard to tell, but from just looking at the code, the script probably haven't completed loading when you try to use it. Since JavaScript is asynchronous, the code continues to run after you create the script element, it will not stop to wait until the script loads.

You will have to wait until the script loads before using its contents. Generally, it's much easier to just use a ' Githubissues.

  • Githubissues is a development platform for aggregating issues.