1

Is there an easy way to see all the markers of my Mapbox map by default, automatically, when the page is just loaded... ? Here is my map : http://www.geometry.be/urbanmaestro/v7/ Thank you so much for help !!

  • This answer might be what you're looking for: https://stackoverflow.com/questions/35586360/mapbox-gl-js-getbounds-fitbounds – BDD Jan 07 '20 at 19:49
  • 1
    Does this answer your question? [Mapbox GL JS getBounds()/fitBounds()](https://stackoverflow.com/questions/35586360/mapbox-gl-js-getbounds-fitbounds) – BDD Jan 07 '20 at 19:49
  • Thank you so much for yours answers. They probably answered to my question :) and I read all the links you gave me and did a lot of tests in my code and I can not apply the solution... http://www.geometry.be/urbanmaestro/v7/test.txt If someone knows where I have to do it in my code, I will be really grateful :-) – Martin Landmeters Jan 14 '20 at 17:29

3 Answers3

3

You need to first calculate the smallest bounding box for your points, i.e figure out the top right and bottom left coordinates that the map would have to zoom into. You could do this manually or use a library like @turf/bbox whose result (minX, minY, maxX, maxY) lets you find these two points' positions.

Now, at any point that the map is initialised, you can call map.fitBounds, passing it the two points as documented, in the order [bottom left, top right].

Marko K
  • 346
  • 3
  • 12
  • Thank you so much for your answer. I did a lot of tests in my code and I can not apply the solution... http://www.geometry.be/urbanmaestro/v7/test.txt If someone knows where I have to do it in my code, I will be really grateful :-) – Martin Landmeters Jan 23 '20 at 11:52
2

Just add this code after add all of your annotations in Mapbox

let inset = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)

guard let annotations = mapView.annotations else {return}

self.mapView.showAnnotations(annotations, edgePadding: inset, animated: true, completionHandler: nil)

This will get all of your annotations in Mapbox and show 50 padding from all insets.

Mileta Dulovic
  • 1,036
  • 1
  • 14
  • 33
D.Poojara
  • 21
  • 5
0

first of all, you should find the bounding area to encompass markers.

you calculate this easily by this code

  locations.map((spot)=>{ // location in [long,lat] format
  if (spot.coordinates[0] > ne[0]){ // ne = norh east
    ne[0] = spot.coordinates[0];
  }
  if (spot.coordinates[1] > ne[1]){
    ne[1] = spot.coordinates[1];
  }
  if (spot.coordinates[0] < sw[0]){ //sw = south west
    sw[0] = spot.coordinates[0];
  }
  if (spot.coordinates[1] < sw[1]){
    sw[1] = spot.coordinates[1];
  }
});
this.mapBound=[ne,sw];
}

then call fitBounds method of mapbox. link

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
AliTN
  • 96
  • 5