javascript – Comprehensive Guide to Mapbox Geocoding and Markers Implementation

geocodingjavascriptmapboxmarkers

I recently added Mapbox's Geocoder Control and it works wonderfully. I was wondering if anyone could tell me how I could have a marker appear at the location of the address that was searched for? Below is the code I'm currently using for the control.

.addControl(L.mapbox.geocoderControl('mapbox.places', {
position: 'topright',
keepOpen: false,
autocomplete: false
}))

Best Answer

Jonatas' answer works great. If you're using the goecode autocomplete, use the 'select' event instead of the 'found' event. Here is how I place a marker for the geocoded result:

    var geocoderControl = L.mapbox.geocoderControl('mapbox.places', {keepOpen: false, autocomplete: true});
    geocoderControl.addTo(map);


    geocoderControl.on('select', function(object){
       var coord = object.feature.geometry.coordinates;
       L.marker([coord[1], coord[0]]).addTo(map);
});