├── .gitignore
├── .travis.yml
├── Gruntfile.js
├── LICENSE.md
├── README.md
├── dist
├── leaflet-realtime.js
└── leaflet-realtime.min.js
├── examples
├── earthquakes.html
├── earthquakes.js
├── index.html
├── index.js
├── trail.html
└── trail.js
├── package-lock.json
├── package.json
├── src
└── L.Realtime.js
└── test
└── L.Realtime.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - 10
4 | addons:
5 | apt:
6 | packages:
7 | - xvfb
8 | install:
9 | - export DISPLAY=':99.0'
10 | - Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
11 | - npm install
12 |
--------------------------------------------------------------------------------
/Gruntfile.js:
--------------------------------------------------------------------------------
1 | module.exports = function(grunt) {
2 | grunt.initConfig({
3 | pkg: grunt.file.readJSON('package.json'),
4 | browserify: {
5 | control: {
6 | src: ['src/L.Realtime.js'],
7 | dest: 'dist/leaflet-realtime.js',
8 | options: {
9 | browserifyOptions: {
10 | standalone: 'L.Realtime'
11 | }
12 | }
13 | }
14 | },
15 | uglify: {
16 | options: {
17 | banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
18 | '<%= grunt.template.today("yyyy-mm-dd") %> */\n\n'
19 | },
20 | build: {
21 | src: 'dist/leaflet-realtime.js',
22 | dest: 'dist/leaflet-realtime.min.js'
23 | }
24 | }
25 | });
26 |
27 | grunt.loadNpmTasks('grunt-browserify');
28 | grunt.loadNpmTasks('grunt-contrib-uglify');
29 | grunt.registerTask('default', ['browserify', 'uglify']);
30 | };
31 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | ## ISC License
2 |
3 | Copyright (c) 2014, Per Liedman (per@liedman.net)
4 |
5 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
8 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Leaflet Realtime
2 |
3 | [](https://travis-ci.org/perliedman/leaflet-realtime)
4 | [](https://www.npmjs.com/package/leaflet-realtime) 
5 | [](https://cdnjs.com/libraries/leaflet-realtime) [](https://greenkeeper.io/)
6 |
7 | Put realtime data on a Leaflet map: live tracking GPS units, sensor data or just about anything.
8 |
9 | _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.
10 |
11 | ## Example
12 |
13 | Checkout the [Leaflet Realtime Demo](http://www.liedman.net/leaflet-realtime). Basic example:
14 |
15 | ```javascript
16 | var map = L.map('map'),
17 | realtime = L.realtime({
18 | url: 'https://wanderdrone.appspot.com/',
19 | crossOrigin: true,
20 | type: 'json'
21 | }, {
22 | interval: 3 * 1000
23 | }).addTo(map);
24 |
25 | realtime.on('update', function() {
26 | map.fitBounds(realtime.getBounds(), {maxZoom: 3});
27 | });
28 | ```
29 |
30 | ## Usage
31 |
32 | ### Overview
33 |
34 | 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](http://socket.io/) or similar.
35 |
36 | 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.
37 |
38 | 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.
39 |
40 | `L.Realtime` can also use other layer types to display the results, for example it can use a `MarkerClusterGroup` from [Leaflet MarkerCluster](https://github.com/Leaflet/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.)
41 |
42 | Typical usage involves instantiating `L.Realtime` with options for [`style`](http://leafletjs.com/reference.html#geojson-style) and/or [`onEachFeature`](http://leafletjs.com/reference.html#geojson-oneachfeature), to customize styling and interaction, as well as adding a listener for the [`update`](#event-update) event, to for example list the features currently visible in the map.
43 |
44 | Since version 2.0, Leaflet Realtime uses the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/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](#source) to make the AJAX requests.
45 |
46 | ### Push data
47 |
48 | 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`.
49 |
50 | Since automatic updates do not make sense in a push scenario, you want to create the layer with the option `start` set to `false`.
51 |
52 | ### API
53 |
54 | #### L.Realtime
55 |
56 | This is a realtime updated layer that can be added to the map. It extends [L.GeoJSON](http://leafletjs.com/reference.html#geojson).
57 |
58 | ##### Creation
59 |
60 | Factory | Description
61 | -----------------------|-------------------------------------------------------
62 | `L.Realtime(<`[`Source`](#source)`> source, <`[`RealtimeOptions`](#realtimeoptions)`> options?)` | Instantiates a new realtime layer with the provided source and options
63 |
64 | ##### Options
65 |
66 | Provides these options, in addition to the options of [`L.GeoJSON`](http://leafletjs.com/reference.html#geojson).
67 |
68 | Option | Type | Default | Description
69 | -----------------------|---------------------|----------------------|---------------------------------------------------------
70 | `start` | `Boolean` | `true` | Should automatic updates be enabled when layer is added on the map and stopped when layer is removed from the map
71 | `interval` | `Number` | 60000 | Automatic update interval, in milliseconds
72 | `getFeatureId( featureData)` | `Function` | Returns `featureData.properties.id` | Function used to get an identifier uniquely identify a feature over time
73 | `updateFeature( featureData, 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 |
74 | `container` | `LayerGroup` | L.geoJson() | Specifies the layer instance to display the results in
75 | `removeMissing` | `Boolean` | `false` | Should missing features between updates been automatically removed from the layer
76 |
77 | ##### Events
78 |
79 | Event | Data | Description
80 | --------------|----------------|---------------------------------------------------------------
81 | `update` | [`UpdateEvent`](#updateevent) | Fires when the layer's data is updated
82 |
83 | ##### Methods
84 |
85 | Method | Returns | Description
86 | -----------------------|----------------|-----------------------------------------------------------------
87 | `start()` | `this` | Starts automatic updates
88 | `stop()` | `this` | Stops automatic updates
89 | `isRunning()` | `Boolean` | Tells if automatic updates are running
90 | `update( 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
91 | `remove( featureData)` | `this` | Removes the provided feature or features from the layer
92 | `getLayer( featureId)` | `ILayer` | Retrieves the layer used for a certain feature
93 | `getFeature( featureId)` | `GeoJSON` | Retrieves the feature data for the given `featureId`
94 |
95 | #### Source
96 |
97 | The source can be one of:
98 |
99 | * a string with the URL to get data from
100 | * an options object that is passed to [fetch](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) for fetching the data
101 | * a function in case you need more freedom.
102 |
103 | In case you use a function, the function should take two callbacks as arguments: `fn(success, error)`, with the callbacks:
104 |
105 | * a success callback that takes GeoJSON as argument: `success( features)`
106 | * an error callback that should take an error object and an error message (string) as argument: `error(