mapbox / mapbox-gl-geocoder

Geocoder control for mapbox-gl-js using Mapbox Geocoding API
https://mapbox.com/mapbox-gl-js/example/mapbox-gl-geocoder/
ISC License
368 stars 180 forks source link

Adding an initial marker #458

Open kpagcha opened 2 years ago

kpagcha commented 2 years ago

I'm trying to use with plugin as a widget to input geolocation of a venue. But I have two requirements:

options.marker accepts a Boolean or an Object. I assumed the intent of the latter was precisely to pass an existing marker instance to use when searching. However, I've tried doing this and no initial marker is being shown on the map:

const map = new mapboxgl.Map({
    container: map,
    style: 'mapbox://styles/mapbox/streets-v11',
    center: coords,
});

map.addControl(new MapboxGeocoder({
    accessToken: mapboxgl.accessToken,
    mapboxgl: mapboxgl,
    coords: new mapboxgl.Marker({draggable: true}).setLngLat(coords)
}), 'top-left');
just-Bri commented 2 years ago

@kpagcha What is the mapboxgl variable? I'm having a different issue but wondering what that variable actually points to, or equals. Trying to figure out if my map is not the correct instance type.

map.addControl(new MapboxGeocoder({
    accessToken: mapboxgl.accessToken,
    mapboxgl: mapboxgl, <= What is this?
    coords: new mapboxgl.Marker({draggable: true}).setLngLat(coords)
}), 'top-left');
kpagcha commented 2 years ago

@just-Bri well that's just how the mapbox-gl-geocoder docs tell you to initialize the component. But mapboxgl is just the object you get when importing the mapbox-gl.js library.

just-Bri commented 2 years ago

@kpagcha As in it's a mapboxgl.Map right? I'm having issues using react-map-gl, but I'm not sure if this specific piece has anything to do with it.

A coworker of mine pointed out that it might be a bad import/export somewhere but I can't find any in our code, haven't checked the source for mapbox-gl-geocoder yet though.