Last active
January 11, 2017 02:38
-
-
Save danswick/df57843e267315dde6d9c4e86ab4532f to your computer and use it in GitHub Desktop.
This file contains hidden or 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></title> | |
| <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> | |
| <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.21.0/mapbox-gl.js'></script> | |
| <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.21.0/mapbox-gl.css' rel='stylesheet' /> | |
| <style> | |
| body { margin:0; padding:0; } | |
| #map { position:absolute; top:0; bottom:0; width:100%; } | |
| .mapboxgl-marker { | |
| width: 2em; | |
| height: 2em; | |
| background-image: url('cat-grumpy-icon.png'); | |
| background-position: center center; | |
| background-size: contain; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="markerEl">omfg</div> | |
| <div id='map'></div> | |
| <script> | |
| mapboxgl.accessToken = 'pk.eyJ1IjoiZGFuc3dpY2siLCJhIjoiY2l1dTUzcmgxMDJ0djJ0b2VhY2sxNXBiMyJ9.25Qs4HNEkHubd4_Awbd8Og'; | |
| var map = new mapboxgl.Map({ | |
| container: 'map', // container id | |
| style: 'mapbox://styles/mapbox/streets-v8', //stylesheet location | |
| center: [-74.50, 40], // starting position | |
| zoom: 9 // starting zoom | |
| }); | |
| var markerEl = document.getElementById('markerEl'); | |
| function addRandomMarker() { | |
| var bounds = map.getBounds(); | |
| var s = bounds.getSouth(); | |
| var n = bounds.getNorth(); | |
| var w = bounds.getWest(); | |
| var e = bounds.getEast(); | |
| var lng = w + (e - w) * Math.random(); | |
| var lat = s + (n - s) * Math.random(); | |
| var marker = new mapboxgl.Marker() | |
| .setLngLat([lng, lat]) | |
| .addTo(map); | |
| var markers = document.getElementsByClassName('mapboxgl-marker'); | |
| markers[i].addEventListener("click", function(e) { | |
| var popup = new mapboxgl.Popup({ | |
| closeButton: false | |
| }); | |
| var coordinates = map.unproject([e.x, e.y]); | |
| popup.setLngLat(coordinates) | |
| .setText('Meow') | |
| .addTo(map); | |
| e.stopPropagation(); | |
| }); | |
| } | |
| for (var i = 0; i < 100; i++) addRandomMarker(); | |
| map.addControl(new mapboxgl.Navigation()); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment