0

I have a Lealfet map together with the Mapbox geocoder outside the map. The Geocoder and the map works. But i need to zoom to the result from the geocoder.

Some code:

          var map = L.map('map').setView([35.936050, 10.541736], 5);


          var tiles = L.tileLayer('http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}.png', {
                maxZoom: 18,
                attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors',
            }).addTo(map);


          var geocoder = new MapboxGeocoder({
           accessToken: mapboxgl.accessToken,
           mapboxgl: mapboxgl,
           marker: false
           });

It just zooms into a random place, when i click a result from the dropdown. In below Example it zooms to a place just outside Somalia

enter image description here

Map result:

enter image description here

Can I somehow log the result from the geocoder, so i can see the data?

  • If you take Amsterdam coordinates lng,lat [52.35, 4.90] and exchange coordinates to [4.90, 52.35], you end up in the sea outside Somalia. Leaflet uses [lat,lng] coordinate order and it seems that Mapboxgeocoder uses [lng,lat] order. – TomazicM Jun 10 at 17:47
  • 1
    I wonder how you managed to mix Leaflet with the Mapbox geocoder (which is meant for mapbox GLJS and not meant for Leaflet). It's not a standard integration AFAIK. – IvanSanchez Jun 10 at 18:22
  • @TomazicM Thanks. Of course! I feel stupid after this..... But it got me on track, somehow, together with the answer here, Mapbox use point result from Geocoding/Search....... . – student Jun 10 at 20:27
1

I solved this based on this mapbox use point result from geocoding together with the comment from @TomazicM.

  1. get the bbox from the geocoder result:

    var geocoder_result = e.result.bbox;

  2. Use fitbounds to fit the map the the bbox:

      map.fitBounds([
        [e.result.bbox[3], e.result.bbox[0]],
        [e.result.bbox[1], e.result.bbox[2]]
      ])

@IvanSanchez Im using the mapbox geocoder "without" a map: Use the geocoder without a map. Then I use the above method to get the result of the geocoder to be used in my Leaflet map.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.