Как рассчитать центр границ без GoogleMaps?

У меня есть следующие границы:

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

Используя API карт Google, я мог рассчитать галоп приведенных выше границ следующим образом:

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

Как можно вычислить центр границ, не используя google.maps.LatLngBounds.getCenter, но математику?

Мне нужно написать «волшебную» функцию, которая возвращает ту же центральную широту, длину, например 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) 

person Erik    schedule 28.03.2015    source источник


Ответы (1)


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

Если вам нужно справиться с пересечением международной линии перемены дат:

Если разница между двумя долготами больше 180 градусов, сместите диапазон от -180 до +180 и от 0 до 360, добавив 360 к каждому числу по модулю 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;
  }

скрипт проверки концепции (отображает результат на карте Google Maps Javascript API v3, но не не требует API)

фрагмент кода::

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);

person geocodezip    schedule 29.03.2015