1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| <html> <head> <title>FOAM Map API Example</title> <script src="https://unpkg.com/deck.gl@^8.0.0/dist.min.js"></script> <script src="https://unpkg.com/latlon-geohash@^1.1.0/latlon-geohash.js"></script> <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.7.0/mapbox-gl.js"></script> <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.7.0/mapbox-gl.css' rel='stylesheet' /> <style type="text/css"> body { width: 100vw; height: 100vh; margin: 0; } #tooltip { position: absolute; z-index: 1; pointer-events: none; color: #FFFFFF; padding: 0 16px; max-width: 320px; border-radius: 12px; background: rgba(38, 171, 95, 0.9) } </style> </head>
<body> <canvas id="deck-canvas"></canvas> <div id="tooltip"></div> </body>
<script type="text/javascript">
const {DeckGL, ScatterplotLayer} = deck;
// Colors const FOAM_PENDING_COLOR = [46, 124, 230]; const FOAM_VERIFIED_COLOR = [38, 171, 95]; const FOAM_CHALLENGED_COLOR = [244, 128, 104]; const FOAM_REMOVED_COLOR = [255, 0, 0]; // Map layout const BOUNDING_BOX = [ [-73.9645, 40.6885], [-73.9845, 40.7085] ]; const INITIAL_ZOOM = 16; const MAX_ZOOM = 16; const RADIUS_SCALE = 50; const RADIUS_MIN_PIXELS = 1; const RADIUS_MAX_PIXELS = 10; // Tooltip var listingHashDisplayed; var fetchingListingHash = false; // Functions function getCenterPoint(bounding_box) { return [(bounding_box[0][0] + bounding_box[1][0]) / 2, (bounding_box[0][1] + bounding_box[1][1]) / 2]; } function getPointColor(state) { if (state && state.status && state.status.type) { if (state.status.type === "applied") { return FOAM_PENDING_COLOR } else if (state.status.type === "listing") { return FOAM_VERIFIED_COLOR } else if (state.status.type === "challenged") { return FOAM_CHALLENGED_COLOR } } else { return FOAM_REMOVED_COLOR } } function getPointCoords(geohash) { coords = Geohash.decode(geohash); return [coords['lon'], coords['lat'], 0]; } // For hovered point, // Load data from external source // Inject data in tooltip function updateTooltipDescription(listingHash) { if (!fetchingListingHash && listingHashDisplayed !== listingHash) { fetchingListingHash = true fetch('https://map-api-direct.foam.space/poi/' + listingHash) .then(response => response.json()) .then((json) => { if (json && json.data && json.data.data) { const el = document.getElementById('tooltip'); const tooltip = `<h3>${json.data.data.name}</h3><p>${json.data.data.description}</p>` el.innerHTML = tooltip; listingHashDisplayed = listingHash fetchingListingHash = false } }); } } // Load data from external source // Use data in a new DeckGL object const DATA_URL = 'https://map-api-direct.foam.space/poi/filtered?swLng=' + BOUNDING_BOX[0][0] + '&swLat=' + BOUNDING_BOX[0][1] + '&neLng=' + BOUNDING_BOX[1][0] + '&neLat=' + BOUNDING_BOX[1][1] + '&status=listing&sort=most_value&limit=100&offset=0' fetch(DATA_URL) .then(response => response.json()) .then(data => new DeckGL({ mapboxApiAccessToken: '<mapbox-access-token>', // Replace with your Mapbox access token mapStyle: 'mapbox://styles/mapbox/dark-v10', initialViewState: { longitude: getCenterPoint(BOUNDING_BOX)[0], latitude: getCenterPoint(BOUNDING_BOX)[1], zoom: INITIAL_ZOOM, maxZoom: MAX_ZOOM, }, layers: [ new ScatterplotLayer({ id: 'scatter-plot', data, radiusScale: RADIUS_SCALE, radiusMinPixels: RADIUS_MIN_PIXELS, radiusMaxPixels: RADIUS_MAX_PIXELS, getPosition: d => getPointCoords(d['geohash']), getFillColor: d => getPointColor(d.state), pickable: true, onHover: ({object, x, y}) => { const el = document.getElementById('tooltip'); if (object) { updateTooltipDescription(object.listingHash); el.style.display = 'block'; el.style.left = x + 'px'; el.style.top = y + 'px'; } else { el.style.display = 'none'; el.innerHTML = ''; listingHashDisplayed = ''; } } }) ] }) );
</script> </html>
|