确定2个坐标之间的中点 [英] Determining Midpoint Between 2 Coordinates
问题描述
我试图确定MKMapView
中两个位置之间的中点.我正在按照此处(和
I am trying to determine the midpoint between two locations in an MKMapView
. I am following the method outlined here (and here) and rewrote it in Objective-C, but the map is being centered somewhere northeast of Baffin Island, which is no where near the two points.
我的方法基于上面链接的java方法:
My method based on the java method linked above:
+(CLLocationCoordinate2D)findCenterPoint:(CLLocationCoordinate2D)_lo1 :(CLLocationCoordinate2D)_loc2 {
CLLocationCoordinate2D center;
double lon1 = _lo1.longitude * M_PI / 180;
double lon2 = _loc2.longitude * M_PI / 100;
double lat1 = _lo1.latitude * M_PI / 180;
double lat2 = _loc2.latitude * M_PI / 100;
double dLon = lon2 - lon1;
double x = cos(lat2) * cos(dLon);
double y = cos(lat2) * sin(dLon);
double lat3 = atan2( sin(lat1) + sin(lat2), sqrt((cos(lat1) + x) * (cos(lat1) + x) + y * y) );
double lon3 = lon1 + atan2(y, cos(lat1) + x);
center.latitude = lat3 * 180 / M_PI;
center.longitude = lon3 * 180 / M_PI;
return center;
}
这两个参数具有以下数据:
The 2 parameters have the following data:
_loc1:
latitude = 45.4959839
longitude = -73.67826455
_loc2:
latitude = 45.482889
longitude = -73.57522299
以上内容正确放置在地图上(蒙特利尔及其周边地区).我试图将地图居中于2之间的中点,但我的方法返回以下内容:
The above are correctly place on the map (in and around Montreal). I am trying to center the map in the midpoint between the 2, yet my method return the following:
latitude = 65.29055
longitude = -82.55425
在北极某处,应该在南500英里左右.
which somewhere in the arctic, when it should be around 500 miles south.
推荐答案
只是预感,但是我注意到您的lon2
和lat2
变量是用M_PI/100
而不是M_PI/180
计算的.
Just a hunch, but I noticed your lon2
and lat2
variables are being computed with M_PI/100
and not M_PI/180
.
double lon1 = _lo1.longitude * M_PI / 180;
double lon2 = _loc2.longitude * M_PI / 100;
double lat1 = _lo1.latitude * M_PI / 180;
double lat2 = _loc2.latitude * M_PI / 100;
将其更改为180可能会有所帮助.
Changing those to 180 might help you out a bit.
这篇关于确定2个坐标之间的中点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!