• Stars
    star
    720
  • Rank 62,908 (Top 2 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created about 10 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Put realtime data on a Leaflet map

Leaflet Realtime

Build status NPM version Leaflet 1.0 compatible! CDNJS Greenkeeper badge

Put realtime data on a Leaflet map: live tracking GPS units, sensor data or just about anything.

Note: version 2 and up of this plugin is _only compatible with Leaflet 1.0 and later. Use earlier versions of Leaflet Realtime if you need Leaflet 0.7 compatibility.

Example

Checkout the Leaflet Realtime Demo. Basic example:

var map = L.map('map'),
    realtime = L.realtime({
        url: 'https://wanderdrone.appspot.com/',
        crossOrigin: true,
        type: 'json'
    }, {
        interval: 3 * 1000
    }).addTo(map);

realtime.on('update', function() {
    map.fitBounds(realtime.getBounds(), {maxZoom: 3});
});

Usage

Overview

By default, Leaflet Realtime reads and displays GeoJSON from a provided source. A "source" is usually a URL, and data can be fetched using AJAX (XHR), JSONP. This means Leaflet Realtime will poll for data, pulling it from the source. Alternatively, you can write your own source, to provide data in just about any way you want. Leaflet Realtime can also be made work with push data, for example data pushed from the server using socket.io or similar.

To be able to figure out when new features are added, when old features are removed, and which features are just updated, Leaflet Realtime needs to identify each feature uniquely. This is done using a feature id. Usually, this can be done using one of the feature's properties. By default, Leaflet Realtime will try to look for a called property id and use that.

By default, L.Realtime uses a L.GeoJSON layer to display the results. You can basically do anything you can do with L.GeoJSON with L.Realtime - styling, onEachFeature, gettings bounds, etc. as if you were working directly with a normal GeoJSON layer.

L.Realtime can also use other layer types to display the results, for example it can use a MarkerClusterGroup from Leaflet MarkerCluster: pass a LayerGroup (or any class that implements addLayer and removeLayer) to L.Realtime's container option. (This feature was added in version 2.1.0.)

Typical usage involves instantiating L.Realtime with options for style and/or onEachFeature, to customize styling and interaction, as well as adding a listener for the update event, to for example list the features currently visible in the map.

Since version 2.0, Leaflet Realtime uses the Fetch API to request data (AJAX). If you are in the unfortunate situation that you need to support a browser without Fetch, you either need to use a polyfill, or write your own source function to make the AJAX requests.

Push data

If you prefer getting data pushed from the server, in contrast to Leaflet Realtime pulling it with a standard HTTP request, you can feed added and updated GeoJSON data to Leaflet Realtime using the update method. In this scenario, you will also need to remove features by explicit calls to remove.

Since automatic updates do not make sense in a push scenario, you want to create the layer with the option start set to false.

API

L.Realtime

This is a realtime updated layer that can be added to the map. It extends L.GeoJSON.

Creation
Factory Description
L.Realtime(<Source> source, <RealtimeOptions> options?) Instantiates a new realtime layer with the provided source and options
Options

Provides these options, in addition to the options of L.GeoJSON.

Option Type Default Description
start Boolean true Should automatic updates be enabled when layer is added on the map and stopped when layer is removed from the map
interval Number 60000 Automatic update interval, in milliseconds
getFeatureId(<GeoJSON> featureData) Function Returns featureData.properties.id Function used to get an identifier uniquely identify a feature over time
updateFeature(<GeoJSON> featureData, <ILayer> oldLayer) Function Special Used to update an existing feature's layer; by default, points (markers) are updated, other layers are discarded and replaced with a new, updated layer. Allows to create more complex transitions, for example, when a feature is updated
container LayerGroup L.geoJson() Specifies the layer instance to display the results in
removeMissing Boolean false Should missing features between updates been automatically removed from the layer
Events
Event Data Description
update UpdateEvent Fires when the layer's data is updated
Methods
Method Returns Description
start() this Starts automatic updates
stop() this Stops automatic updates
isRunning() Boolean Tells if automatic updates are running
update(<GeoJSON> featureData?) this Updates the layer's data. If featureData is provided, it is used to add or update data in the layer, otherwise the layer's source is queried for new data asynchronously
remove(<GeoJSON> featureData) this Removes the provided feature or features from the layer
getLayer(<FeatureId> featureId) ILayer Retrieves the layer used for a certain feature
getFeature(<FeatureId> featureId) GeoJSON Retrieves the feature data for the given featureId

Source

The source can be one of:

  • a string with the URL to get data from
  • an options object that is passed to fetch for fetching the data
  • a function in case you need more freedom.

In case you use a function, the function should take two callbacks as arguments: fn(success, error), with the callbacks:

  • a success callback that takes GeoJSON as argument: success(<GeoJSON> features)
  • an error callback that should take an error object and an error message (string) as argument: error(<Object> error, <String> message)

UpdateEvent

An update event is fired when the layer's data is updated from its source. The data included loosely resembles D3's join semantics, to make it easy to handle new features (the enter set), updated features (the update set) and removed features (the exit set).

property type description
features Object Complete hash of current features, with feature id as key
enter Object Features added by this update, with feature id as key
update Object Existing features updated by this update, with feature id as key
exit Object Existing features that were removed by this update, with feature id as key

More Repositories

1

leaflet-routing-machine

Control for routing in Leaflet
JavaScript
1,070
star
2

leaflet-control-geocoder

A simple geocoder form to locate places. Easily extended to multiple data providers.
TypeScript
510
star
3

geojson-path-finder

Find shortest path through a network of GeoJSON
TypeScript
270
star
4

reproject

Change, convert, transform, reproject GeoJSON between different projections / CRS
JavaScript
212
star
5

query-overpass

Make queries to OpenStreetMap's overpass API and output as GeoJSON
JavaScript
147
star
6

raytracing-in-one-weekend

Rust implementation of the book Ray Tracing in One Weekend
Rust
94
star
7

tiled-maps

A short, free and open introduction to the concept of tiled maps
HTML
61
star
8

shadow-mapper

Generate maps of sun and shade from OpenStreetMap data
Python
56
star
9

elevation-service

Elevation data for your GeoJSON as a micro service
JavaScript
46
star
10

node-hgt

Query hgt files (typically SRTM elevation data) with performance
JavaScript
45
star
11

geojson-elevation

Add juicy elevation data to your fresh GeoJSON
JavaScript
38
star
12

ocad2geojson

JavaScript OCAD file reader and exporter for GeoJSON, SVG and Mapbox Style Spec
JavaScript
32
star
13

lrm-graphhopper

Support for GraphHopper in Leaflet Routing Machine
JavaScript
31
star
14

leaflet-underneath

Find interesting features is in your map using Mapbox Vector Tiles data
JavaScript
30
star
15

openlayers-routing-machine

A complete user interface for routing within an OpenLayers map
HTML
18
star
16

geojson2obj

Convert GeoJSON into Wavefront OBJ format
JavaScript
17
star
17

leaflet-touch-helper

Make it easy to touch things in Leaflet
JavaScript
17
star
18

osm-slope

Calculate slope (climb and descent) for highway ways in OpenStreetMap data.
JavaScript
17
star
19

svenska-landskap

Sveriges landskap som öppen geodata i GeoJSON
Shell
13
star
20

terrain-obj

Convert elevation data (typically SRTM elevation data, HGT files) to Wavefront OBJ 3D models
JavaScript
12
star
21

gis-nerd-tools

Simple but awesome web based tools for GIS enthusiasts
JavaScript
10
star
22

o-scout

A web based app for orienteering course setting and exploration
JavaScript
9
star
23

osmwave

Convert OpenStreetMap buildings and roads into Wavefront OBJ 3D models.
C++
7
star
24

leaflet-multi-style

Quickly add multiple styles to GeoJSON data
JavaScript
7
star
25

leaflet-raster

A simple Leaflet plugin for powerful raster functions
JavaScript
7
star
26

raster-blaster

Blast multi-band rasters to a canvas with speed and style.
JavaScript
6
star
27

cykelbanor

Bike routing for Sweden
JavaScript
5
star
28

gps-log-viewer

Web UI from gps-log
JavaScript
5
star
29

standstill

Find locations where there has been no movement, a stop, within a GeoJSON track, typically recorded from a GPS
JavaScript
5
star
30

fly-route

Fly over a map route using Mapbox GL (small demo)
JavaScript
4
star
31

course-sketcher

Web based course setting for orienteering
Vue
4
star
32

lrm-mapbox

DEPRECATED. Support for Mapbox directions API v4 in Leaflet Routing Machine
JavaScript
4
star
33

osm2obj

Generate Wavefront OBJ 3D models from OpenStreetMap XML or JSON
JavaScript
4
star
34

openlayers-tilejson

Create map or tile layer from TileJSON, with projection support
JavaScript
4
star
35

css-rankme-slack-bot

Slack bot to inform about important stats from Counter Strike Source's Rankme plugin
Python
3
star
36

lrm-svelte

Experiment making a Leaflet plugin using the Svelte framework
JavaScript
3
star
37

maptime-gbg-201602

"Let's learn Leaflet.js" presentation for Maptime GBG 2016-02
JavaScript
3
star
38

local-proj

Find a suitable local projection from GeoJSON data
JavaScript
3
star
39

brochure

Simple GeoJSON viewer based on Leaflet
JavaScript
3
star
40

jsprit.web

Server side of a simple Vechile Routing Problem solver built on jsprit
Java
2
star
41

mapbox-gl-routing-machine

A complete user interface for routing with Mapbox GL JS
JavaScript
2
star
42

roundabound

Simplistic log rotation utility
Python
2
star
43

ocad2tiles

Create raster images and tiles (image pyramids) from OCAD map files
JavaScript
2
star
44

advent-of-code

My solutions for Advent of Code
Python
2
star
45

svg-control-descriptions

IOF orienteering control descriptions as SVG
JavaScript
2
star
46

tradportalen-export

Export tree observations from Trädportalen
Shell
1
star
47

turfjs-gbgnodejs

Short introduction to turf.js @ gbgnodejs meetup
JavaScript
1
star
48

drone

Enjoy Mapbox's cloudless atlas
JavaScript
1
star
49

godwit

Minimalistic database migration tool
Python
1
star
50

vixel-test

Toying around with making voxel landscapes from height maps
JavaScript
1
star
51

memory-game

Very basic memory game I made for my kids
JavaScript
1
star
52

gps-track-import

Simple import of tracks for GPS
Python
1
star