R
R
Roman Andreevich2020-12-15 12:47:05
Google Maps
Roman Andreevich, 2020-12-15 12:47:05

How to get area angles from google maps markers array?

Colleagues, good day, who came across, I ask for help, the essence is this:

There is an array of coordinates from objects { lat, lng } at the input, you need to get this array using the googla maps api method (I can’t find a solution) to get the coordinates of the corners ( bounds ) that to get the center of the map between these points. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Salavat Salakhutdinov, 2020-12-15
@Jackardios

function calculateBounds(points) {
  const amount = points.length;
  if (amount === 0) {
    return [];
  }
  let northeast = Object.assign({}, points[0]);
  let southwest = Object.assign({}, points[0]);
  for (let i = 1; i < amount; ++i) {
    let point = points[i];
    ['lat', 'lng'].forEach((c) => {
      northeast[c] = Math.min(northeast[c], point[c]);
      southwest[c] = Math.max(southwest[c], point[c]);
    });
  }
  return new google.maps.LatLngBounds(
    new google.maps.LatLng(northeast.lat,northeast.lng),
    new google.maps.LatLng(southwest.lat,southwest.lng)
  );
}
const points = [
  {lat: 55.3, lng: 36.24},
  {lat: 53.1, lng: 35.39},
  {lat: 57.7, lng: 39.16}
];
const bounds = calculateBounds(points);
bounds.getCenter();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question