How to calculate center of bounds without GoogleMaps?
Asked Answered
C

1

7

I have the following bounds:

var bounds = {
    southwest: {lat: 54.69726685890506, lng: -2.7379201682812226},
    northeast: {lat: 55.38942944437183, lng: -1.2456105979687226}
};

By using google maps API I could calculate canter of the bounds above like the following:

// returns (55.04334815163844, -1.9917653831249726)
(new google.maps.LatLngBounds(bounds.southeast, bounds.northeast)).getCenter();

How could calculate center of bounds without using google.maps.LatLngBounds.getCenter but Math?

I need to write "magic" function that returns the same center lat, lng like google.maps.LatLngBounds.getCenter:

function getBoundsCenter(bounds) {
    // need to calculate and return center of passed bounds;    
}

var center = getBoundsCenter(bounds); // center should be (55.04334815163844, -1.9917653831249726) 
Cyanite answered 28/3, 2015 at 21:2 Comment(0)
T
20
var bounds = {
    southwest: {lat: 54.69726685890506, lng: -2.7379201682812226},
    northeast: {lat: 55.38942944437183, lng: -1.2456105979687226}
};

center lat = (southwest.lat + northeast.lat)/2 = 55.043348151638
center lng = (southwest.lng + northeast.lng)/2 = -1.991765383125

If you need to handle crossing the International Date Line:

If the difference between the two longitudes is greater than 180 degrees, shift the range from -180 to +180 to 0 to 360 by adding 360 to each number modulo 360:

  if ((bounds.southwest.lng - bounds.northeast.lng > 180) || 
      (bounds.northeast.lng - bounds.southwest.lng > 180))
  {
    bounds.southwest.lng += 360;
    bounds.southwest.lng %= 360;
    bounds.northeast.lng += 360;
    bounds.northeast.lng %= 360;
  }

proof of concept fiddle (displays the result on a Google Maps Javascript API v3 map, but doesn't require the API)

code snippet::

console.log("original bounds in question");
var bounds = {
  southwest: {
    lat: 54.69726685890506,
    lng: -2.7379201682812226
  },
  northeast: {
    lat: 55.38942944437183,
    lng: -1.2456105979687226
  }
};

if ((bounds.southwest.lng - bounds.northeast.lng > 180) || (bounds.northeast.lng - bounds.southwest.lng > 180)) {
  bounds.southwest.lng += 360;
  bounds.southwest.lng %= 360;
  bounds.northeast.lng += 360;
  bounds.northeast.lng %= 360;
}
var center_lat = (bounds.southwest.lat + bounds.northeast.lat) / 2; // = 55.043348151638
console.log("center_lat=" + center_lat);
var center_lng = (bounds.southwest.lng + bounds.northeast.lng) / 2; // = -1.991765383125
console.log("center_lng=" + center_lng);

console.log("bounds in crossing International Date Line");
var bounds = {
  southwest: {
    lat: 54.69726685890506,
    lng: -182.7379201682812226
  },
  northeast: {
    lat: 55.38942944437183,
    lng: 181.2456105979687226
  }
};

if ((bounds.southwest.lng - bounds.northeast.lng > 180) || (bounds.northeast.lng - bounds.southwest.lng > 180)) {
  bounds.southwest.lng += 360;
  bounds.southwest.lng %= 360;
  bounds.northeast.lng += 360;
  bounds.northeast.lng %= 360;
}
var center_lat = (bounds.southwest.lat + bounds.northeast.lat) / 2; // = 55.043348151638
console.log("center_lat=" + center_lat);
var center_lng = (bounds.southwest.lng + bounds.northeast.lng) / 2; // = -1.991765383125
console.log("center_lng=" + center_lng);
Ted answered 29/3, 2015 at 0:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.