FOAM API Example - Mapbox.GL
-
-
Save kejace/c312c8ba8f05c910b6f09dcc81212bd8 to your computer and use it in GitHub Desktop.
FOAM API Example - Mapbox.GL
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
license: mit |
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> | |
<meta charset='utf-8' /> | |
<title>FOAM API Example - Mapbox.GL</title> | |
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.css' rel='stylesheet' /> | |
<script src="https://unpkg.com/axios/dist/axios.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/latlon-geohash@1.1.0/latlon-geohash.min.js"></script> | |
<title>API Demo</title> | |
<style> | |
body { margin:0; padding:0; } | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script> | |
//see this in action: http://bl.ocks.org/FergusDevelopmentLLC/e1cb1d18dac41c46c72d8c19f7ef09c8 | |
// get bounding box: http://bboxfinder.com | |
let mapBounds = [-105.244904, 39.515695, -104.648895, 39.985538];//Southwest corner, Northeast corner | |
let url = 'https://map-api-direct.foam.space:443/poi/map?swLng=' + mapBounds[0] + '&swLat=' + mapBounds[2] + '&neLng=' + mapBounds[1] + '&neLat=' + mapBounds[3]; | |
// let basemap = 'basic'; | |
// let basemap = 'streets'; | |
// let basemap = 'bright'; | |
// let basemap = 'light'; | |
let basemap = 'dark'; | |
// let basemap = 'satellite'; | |
// https://www.mapbox.com/api-documentation/#styles | |
let map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'mapbox://styles/mapbox/' + basemap + '-v9', | |
center: [(mapBounds[0] + mapBounds[2]) / 2, (mapBounds[1] + mapBounds[3]) / 2], | |
zoom: 12 | |
}); | |
mapboxgl.accessToken = 'pk.eyJ1Ijoid2lsbGNhcnRlciIsImEiOiJjamV4b2g3Z2ExOGF4MzFwN3R1dHJ3d2J4In0.Ti-hnuBH8W4bHn7k6GCpGw'; | |
map.on("load", function () { | |
axios.get(url) | |
.then((response) => { | |
let data = response.data; | |
let features = []; | |
for (beacon in data) { | |
//decode the beaconGeoHash value to lat, lon | |
let decodedLatLng = Geohash.decode(data[beacon].geohash); | |
feature = { | |
type: 'Feature', | |
geometry: { | |
type: 'Point', | |
coordinates: [decodedLatLng.lon, decodedLatLng.lat] | |
}, | |
properties: { | |
beaconTransactionHash: data[beacon].listingHash, | |
beaconName: data[beacon].owner, | |
beaconGeohash: data[beacon].geohash, | |
beaconBeaconAddress: data[beacon].owner, | |
beaconFactoryAddress: data[beacon].owner | |
} | |
}; | |
features.push(feature); | |
} | |
let geojson = { | |
type: 'FeatureCollection', | |
features: features | |
}; | |
map.addSource('data', { | |
type: 'geojson', | |
data: geojson | |
}); | |
map.addLayer({ | |
id: 'points', | |
source: 'data', | |
type: 'circle', | |
paint: { | |
'circle-radius': 5, | |
'circle-color': 'yellow' | |
} | |
}) | |
}, | |
(error) => { | |
let status = error.response.status | |
} | |
); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment