How to calculate the correct zoom based in the distance between two points?

  google-maps, javascript, leaflet, maps

I’m trying to build a dynamic zoom behavior for a map, based on the distance between two coordinates, for instance:

const coord1 = {
  lat: a,
  lng: b
}

const coord2 = {
  lat: c,
  lng: d
}

As default, I’m creating a map using:

const map = L.map('map', {
    zoom: 16
});

What I want to achieve is to dynamically change the zoom based on the distance between coord1 and coord2, so that the markers I will put there appears in the map.

Source: Ask Javascript Questions

LEAVE A COMMENT