Last active
September 24, 2017 10:38
-
-
Save marcus-crane/19baa81c853580068e2cd9dc11aa7afc to your computer and use it in GitHub Desktop.
NEC Smart Cities sensor map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>NEC Smart Cities Sensors</title> | |
<link | |
rel="stylesheet" | |
href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" | |
integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ==" | |
crossorigin="" | |
/> | |
<style> | |
#mapid { height: 450px; width: 100%; } | |
</style> | |
<script | |
src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js" | |
integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log==" | |
crossorigin=""> | |
</script> | |
</head> | |
<body> | |
<div id="mapid"></div> | |
<script type="text/javascript"> | |
const wellington = L.map('mapid').setView([-41.2934341, 174.7812477], 15) | |
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', { | |
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Sensor data © <a href="http://api.necsmart.city/v0.1">NEC Smart Cities</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>', | |
maxZoom: 18, | |
id: 'mapbox.streets', | |
accessToken: '<mapbox-token>' | |
}).addTo(wellington) | |
fetch('https://api.necsmart.city/v0.1/sensors?token=<nec-token>') | |
.then(response => response.json()) | |
.then(data => { | |
let sensors = data.sensors | |
let uniqueSensors = new Set() | |
let mergedSensors = [] | |
sensors.map(sensor => uniqueSensors.add(sensor.kite_name)) | |
for (let sensor_name of uniqueSensors) { | |
let sensorsToMerge = sensors.filter(sensor => sensor.kite_name === sensor_name) | |
let functions = sensorsToMerge.reduce((a, b) => a.concat(b.sensor_name), []) | |
mergedSensors.push({ | |
"kite_name": sensor_name, | |
"functions": functions, | |
"location": { | |
"latitude": sensorsToMerge[0].location.latitude, | |
"longitude": sensorsToMerge[0].location.longitude | |
} | |
}) | |
} | |
mergedSensors.map(sensor => { | |
L.marker([sensor.location.latitude, sensor.location.longitude]) | |
.bindPopup(`Hi, I'm ${sensor.kite_name} and I measure the following: <ul>${sensor.functions.map(func =>`<li>${func}</li>`).join('')}</ul>`) | |
.addTo(wellington) | |
}) | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use this code, you'll see API keys from