Monday, 1 October 2018

javascript - Mapbox Geocoding and Markers


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 }))



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...