Skip to content

Instantly share code, notes, and snippets.

@andrewharvey
Last active June 11, 2018 09:36
Show Gist options
  • Save andrewharvey/f388e9197eafa62aa118535787a74825 to your computer and use it in GitHub Desktop.
Save andrewharvey/f388e9197eafa62aa118535787a74825 to your computer and use it in GitHub Desktop.
Mapbox GL Geocoder without camera animation
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Add a geocoder</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.43.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.43.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.1.2/mapbox-gl-geocoder.min.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.1.2/mapbox-gl-geocoder.css' type='text/css' />
<div id='map'></div>
<script>
mapboxgl.accessToken = '';
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,
flyTo: false
});
geocoder.on('result', function (e) {
var result = e.result
if (result.bbox) {
var bbox = result.bbox;
map.fitBounds([[bbox[0], bbox[1]],[bbox[2], bbox[3]]], { duration: 0 });
} else {
map.flyTo({
center: result.center,
zoom: 16,
duration: 0
});
}
});
map.addControl(geocoder);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment