Skip to content

Instantly share code, notes, and snippets.

@iampato
Created November 21, 2020 09:36
Show Gist options
  • Select an option

  • Save iampato/22189f8b44c610ff5bf577177b5d1569 to your computer and use it in GitHub Desktop.

Select an option

Save iampato/22189f8b44c610ff5bf577177b5d1569 to your computer and use it in GitHub Desktop.
how to add mapbox markers by location name
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiaWFtcGF0byIsImEiOiJja2g0aTBtZWwwNWh6MnhscHphazdxYjJmIn0.YTbOQsa6x1IJ3nLjyAoQ1w';
var mapboxClient = mapboxSdk({ accessToken: mapboxgl.accessToken });
mapboxClient.geocoding
.forwardGeocode({
query: 'Wellington, New Zealand',
autocomplete: false,
limit: 1
})
.send()
.then(function (response) {
if (
response &&
response.body &&
response.body.features &&
response.body.features.length
) {
var feature = response.body.features[0];
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: feature.center,
zoom: 10
});
new mapboxgl.Marker().setLngLat(feature.center).addTo(map);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment