一搜全是計(jì)算兩個(gè)坐標(biāo)之間距離的算法, 數(shù)學(xué)不行, 自己對(duì)算法不是很精通, 折騰了好幾天才寫出來(lái)的, 如果有錯(cuò)誤請(qǐng)留言謝謝
代碼如下
/**
通過(guò) A 點(diǎn)經(jīng)緯度增加公里數(shù)得到 B 點(diǎn)經(jīng)緯度
@param centerCoordinate? A 點(diǎn)坐標(biāo)
@param distance 公里數(shù)(km)
@param radians? 正北方向(0 ~ 360)
@return 返回值
*/
- (CLLocationCoordinate2D)calculateLatLng:(CLLocationCoordinate2D)centerCoordinate Distance:(CLLocationDistance)distance Radians:(CLLocationDistance)radians
{
static double Rc = 6378137;// 地球的赤道半徑
static double Rj = 6356725;// 極半徑
double m_LoDeg,m_LoMin,m_LoSec;
double m_LaDeg,m_LaMin,m_LaSec;
double m_Longitude,m_Latitude;
double m_RadLo,m_RadLa;
double Ec;
double Ed;
m_LaDeg = centerCoordinate.latitude;
m_LaMin = (centerCoordinate.latitude - m_LaDeg) * 60;
m_LaSec = (centerCoordinate.latitude - m_LaDeg-m_LaMin / 60) * 3600;
m_LoDeg = centerCoordinate.longitude;
m_LoMin = (centerCoordinate.longitude - m_LoDeg) * 60;
m_LoSec = (centerCoordinate.longitude - m_LoDeg - m_LoMin / 60) * 3600;
m_Longitude = centerCoordinate.longitude;
m_Latitude = centerCoordinate.latitude;
m_RadLo = centerCoordinate.longitude * M_PI / 180.;
m_RadLa = centerCoordinate.latitude * M_PI / 180.;
Ec = Rj + (Rc - Rj) * (90 - m_Latitude) / 90.;
Ed = Ec * cos(cos(M_PI / (180 / m_RadLa)));
double dx = distance * 1000 * sin(M_PI / (180 / radians));
double dy = distance * 1000 * cos(M_PI / (180 / radians));
CLLocationDegrees latitude = (dx / Ed + m_RadLo) * 180 / M_PI;
CLLocationDegrees longitude = (dy / Ec + m_RadLa) * 180 / M_PI;
CLLocationCoordinate2D newCoordinate;
newCoordinate.latitude = longitude;
newCoordinate.longitude = latitude;
return newCoordinate;
}