mapbox-gl-geocoder icon indicating copy to clipboard operation
mapbox-gl-geocoder copied to clipboard

Adding an initial marker

Open kpagcha opened this issue 3 years ago • 3 comments

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

  • the marker should be draggable so users can make small changes to the pin position suggested by geocoder if they wish so
  • if a venue is being edited and it already has a position, the map should reflect that by showing an initial marker, bound to the geolocation search

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');

kpagcha avatar May 13 '22 15:05 kpagcha

@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');

just-Bri avatar May 18 '22 18:05 just-Bri

@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.

kpagcha avatar May 18 '22 19:05 kpagcha

@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.

just-Bri avatar May 19 '22 01:05 just-Bri