Skip to content

Instantly share code, notes, and snippets.

@tristen
Created December 22, 2016 15:56
Show Gist options
  • Save tristen/a09627f01d3a3bc54139d52a5eb01386 to your computer and use it in GitHub Desktop.
Save tristen/a09627f01d3a3bc54139d52a5eb01386 to your computer and use it in GitHub Desktop.
Attach Mapbox GL JS controls to arbitrary DOM nodes
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.29.0/mapbox-gl.css' rel='stylesheet' />
<link href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.0.1/mapbox-gl-geocoder.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
.map { position:absolute; top:0; bottom:0; width:100%; }
.geocoder {
position:absolute;
z-index:1;
width:50%;
left:50%;
margin-left:-25%;
top:20px;
}
.mapboxgl-ctrl-geocoder { min-width:100%; }
</style>
</head>
<body>
<div id='map' class='map'></div>
<div id='geocoder' class='geocoder'></div>
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.0.1/mapbox-gl-geocoder.js'></script>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.29.0/mapbox-gl.js'></script>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoidHJpc3RlbiIsImEiOiJiUzBYOEJzIn0.VyXs9qNWgTfABLzSI3YcrQ';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [-79.4512, 43.6568],
zoom: 13
});
var geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken
});
document.getElementById('geocoder').appendChild(geocoder.onAdd(map))
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment