Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapbox-gl-geocoder v4 examples #8053

Merged
30 changes: 30 additions & 0 deletions docs/pages/example/mapbox-gl-geocoder-custom-render.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.0.0/mapbox-gl-geocoder.min.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.0.0/mapbox-gl-geocoder.css' type='text/css' />
<style>
.result{
font-style: italic;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
color: gray;
}
</style>
<div id='map'></div>

<script>
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,
render: function(item){
// add a custom render function that shows an icon in the dropdown menu
var maki = item.properties.maki || 'marker';
return `<span class='result'><img src='https://unpkg.com/@mapbox/[email protected]/icons/${marker}-11.svg'>${item.place_name}</span>`
}
});
map.addControl(geocoder);

</script>
13 changes: 13 additions & 0 deletions docs/pages/example/mapbox-gl-geocoder-custom-render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*---
title: Add a custom render function to a geocoder
description: >-
Use the [mapbox-gl-geocoder](https://github.com/mapbox/mapbox-gl-geocoder)
control to search for places using Mapbox Geocoding API, and render the matching
places in a customized way.
tags:
- geocoder
pathname: /mapbox-gl-js/example/mapbox-gl-geocoder-custom-render/
---*/
import Example from '../../components/example';
import html from './mapbox-gl-geocoder-custom-render.html';
export default Example(html);
16 changes: 2 additions & 14 deletions docs/pages/example/mapbox-gl-geocoder-proximity-bias.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,9 @@
});

var geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken
accessToken: mapboxgl.accessToken,
trackProximity: true
});
map.addControl(geocoder);

map.on('load', updateGeocoderProximity); // set proximity on map load
map.on('moveend', updateGeocoderProximity); // and then update proximity each time the map moves

function updateGeocoderProximity() {
// proximity is designed for local scale, if the user is looking at the whole world,
// it doesn't make sense to factor in the arbitrary centre of the map
if (map.getZoom() > 9) {
var center = map.getCenter().wrap(); // ensures the longitude falls within -180 to 180 as the Geocoding API doesn't accept values outside this range
geocoder.setProximity({ longitude: center.lng, latitude: center.lat });
} else {
geocoder.setProximity(null);
}
}
</script>
19 changes: 19 additions & 0 deletions docs/pages/example/mapbox-gl-geocoder-with-language.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.0.0/mapbox-gl-geocoder.min.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.0.0/mapbox-gl-geocoder.css' type='text/css' />
<div id='map'></div>

<script>
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,
language: 'de-DE'
});
map.addControl(geocoder);

</script>
12 changes: 12 additions & 0 deletions docs/pages/example/mapbox-gl-geocoder-with-language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*---
title: Localize the geocoder to a given language
description: >-
Localize the [mapbox-gl-geocoder](https://github.com/mapbox/mapbox-gl-geocoder)
to set the UI language and improve result relevance in that language.
tags:
- geocoder
pathname: /mapbox-gl-js/example/mapbox-gl-geocoder-with-language/
---*/
import Example from '../../components/example';
import html from './mapbox-gl-geocoder-with-language.html';
export default Example(html);
34 changes: 6 additions & 28 deletions docs/pages/example/point-from-geocoder-result.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [-79.4512, 43.6568],
zoom: 13
zoom: 13,
addToMap: true,
markerOptions: {
color: 'orange',
draggable: true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning behind draggable: true? I get it shows options you can pass in, but I'm not sure if this makes sense as a default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entirely as an example of being able to pass in options. Perhaps just the color option would suffice as an illustrative example.

}
});

var geocoder = new MapboxGeocoder({
Expand All @@ -22,31 +27,4 @@

map.addControl(geocoder);

// After the map style has loaded on the page, add a source layer and default
// styling for a single point.
map.on('load', function() {
map.addSource('single-point', {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": []
}
});

map.addLayer({
"id": "point",
"source": "single-point",
"type": "circle",
"paint": {
"circle-radius": 10,
"circle-color": "#007cbf"
}
});

// Listen for the `result` event from the MapboxGeocoder that is triggered when a user
// makes a selection and add a symbol that matches the result.
geocoder.on('result', function(ev) {
map.getSource('single-point').setData(ev.result.geometry);
});
});
</script>