Skip to content

Instantly share code, notes, and snippets.

@kejace
Forked from FergusDevelopmentLLC/.block
Last active May 9, 2019 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kejace/0d93e33c0da9696b0fc5db3b0fbae06d to your computer and use it in GitHub Desktop.
Save kejace/0d93e33c0da9696b0fc5db3b0fbae06d to your computer and use it in GitHub Desktop.
FOAM API Example - Leaflet
license: mit

FOAM API Example - Leaflet

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>FOAM API Example - Leaflet</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<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>
<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/3b3fd8491b3df85e40d6e0d4b9911493
//get bounding box: http://bboxfinder.com
let mapBounds = [
[39.580819,-105.154953], //Southwest corner
[39.941857,-104.646835] //Northeast corner
];
//create a Leaflet map, assign it to map div, fit it to maxBounds
let map = L.map('map').fitBounds(mapBounds);
//add basemap
//check out more basemaps here: http://leaflet-extras.github.io/leaflet-providers/preview/
let tiles = L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/terrain/{z}/{x}/{y}.{ext}', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
subdomains: 'abcd',
minZoom: 0,
maxZoom: 18,
ext: 'png'
}).addTo(map);
let url = 'https://map-api-direct.foam.space:443/poi/map?swLon=' + mapBounds[0][1] + '&swLat=' + mapBounds[0][0] + '&nwLon=' + mapBounds[1][1] + '&nwLat=' + mapBounds[1][0];
axios.get(url)
.then((response) => {
let beacons = response.data;
//for each beacon in the response from FOAM api
for (beacon in beacons) {
//decode the beaconGeoHash value to lat, lon
//https://github.com/chrisveness/latlon-geohash
let decodedLatLng = Geohash.decode(beacons[beacon].geohash);
//use decoded lat/lon to create Markers for map
let latlng = L.latLng({ lat: decodedLatLng.lat, lng: decodedLatLng.lon });
let marker = L.marker(latlng).addTo(map);
}
},
(error) => {
let status = error.response.status
}
);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment