Created
November 21, 2020 09:36
-
-
Save iampato/22189f8b44c610ff5bf577177b5d1569 to your computer and use it in GitHub Desktop.
how to add mapbox markers by location name
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
| <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