Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 如何在没有谷歌地图的情况下计算边界中心?_Google Maps_Latitude Longitude_Bounds - Fatal编程技术网

Google maps 如何在没有谷歌地图的情况下计算边界中心?

Google maps 如何在没有谷歌地图的情况下计算边界中心?,google-maps,latitude-longitude,bounds,Google Maps,Latitude Longitude,Bounds,我有以下界限: var bounds = { southwest: {lat: 54.69726685890506, lng: -2.7379201682812226}, northeast: {lat: 55.38942944437183, lng: -1.2456105979687226} }; 通过使用google maps API,我可以计算上述边界的坎特,如下所示: // returns (55.04334815163844, -1.9917653831249726)

我有以下界限:

var bounds = {
    southwest: {lat: 54.69726685890506, lng: -2.7379201682812226},
    northeast: {lat: 55.38942944437183, lng: -1.2456105979687226}
};
通过使用google maps API,我可以计算上述边界的坎特,如下所示:

// returns (55.04334815163844, -1.9917653831249726)
(new google.maps.LatLngBounds(bounds.southeast, bounds.northeast)).getCenter();
如果不使用
google.maps.LatLngBounds.getCenter
而不使用数学,如何计算边界中心

我需要编写“magic”函数,返回相同的中心lat,lng,比如
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) 
如果您需要处理跨越国际日期线的问题:

如果两个经度之间的差值大于180度,则通过向每个360模数添加360,将范围从-180到+180到0到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;
  }
(在谷歌地图Javascript API v3地图上显示结果,但不需要API)

代码片段:

console.log(“有问题的原始边界”);
变量界限={
西南部:{
纬度:54.69726685890506,
液化天然气:-2.7379201682812226
},
东北部:{
纬度:55.38942944437183,
液化天然气:-1.2456105979687226
}
};
如果((bounds.southwest.lng-bounds.northeast.lng>180)| |(bounds.northeast.lng-bounds.southwest.lng>180)){
bounds.soutwest.lng+=360;
bounds.southwest.lng%=360;
bounds.northeast.lng+=360;
边界.东北.液化天然气%=360;
}
var center_lat=(bounds.soutwest.lat+bounds.northeast.lat)/2;//=55.043348151638
console.log(“center\u lat=“+center\u lat”);
var center_lng=(bounds.soutwest.lng+bounds.northeast.lng)/2;//=-1.991765383125
console.log(“center_lng=“+center_lng”);
console.log(“跨越国际日期线的边界”);
变量界限={
西南部:{
纬度:54.69726685890506,
液化天然气:-182.7379201682812226
},
东北部:{
纬度:55.38942944437183,
液化天然气:181.2456105979687226
}
};
如果((bounds.southwest.lng-bounds.northeast.lng>180)| |(bounds.northeast.lng-bounds.southwest.lng>180)){
bounds.soutwest.lng+=360;
bounds.southwest.lng%=360;
bounds.northeast.lng+=360;
边界.东北.液化天然气%=360;
}
var center_lat=(bounds.soutwest.lat+bounds.northeast.lat)/2;//=55.043348151638
console.log(“center\u lat=“+center\u lat”);
var center_lng=(bounds.soutwest.lng+bounds.northeast.lng)/2;//=-1.991765383125
console.log(“center_lng=“+center_lng”)
  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;
  }