如何在没有谷歌地图的情况下计算边界中心?
How to calculate center of bounds without GoogleMaps?
我有以下界限:
var bounds = {
southwest: {lat: 54.69726685890506, lng: -2.7379201682812226},
northeast: {lat: 55.38942944437183, lng: -1.2456105979687226}
};
通过使用 google 地图 API 我可以像下面这样计算上面边界的中心:
// returns (55.04334815163844, -1.9917653831249726)
(new google.maps.LatLngBounds(bounds.southeast, bounds.northeast)).getCenter();
如何在不使用 google.maps.LatLngBounds.getCenter
而使用数学的情况下计算边界中心?
我需要编写 "magic" 函数 returns 与 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)
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;
}
proof of concept fiddle(在 Google 地图 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);
我有以下界限:
var bounds = {
southwest: {lat: 54.69726685890506, lng: -2.7379201682812226},
northeast: {lat: 55.38942944437183, lng: -1.2456105979687226}
};
通过使用 google 地图 API 我可以像下面这样计算上面边界的中心:
// returns (55.04334815163844, -1.9917653831249726)
(new google.maps.LatLngBounds(bounds.southeast, bounds.northeast)).getCenter();
如何在不使用 google.maps.LatLngBounds.getCenter
而使用数学的情况下计算边界中心?
我需要编写 "magic" 函数 returns 与 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)
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;
}
proof of concept fiddle(在 Google 地图 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);