? ? ?之前一直做的是圓形電子圍欄醒第,相對比較簡單,因為第三方地圖都有借口可以繪制进鸠;但第三方地圖繪制多邊形都需要每個拐角點都要經(jīng)緯度稠曼,這個跟我現(xiàn)在的項目不符合;因為我只能得到一個經(jīng)緯點客年。
因為電子圍欄的范圍是我們設(shè)置的霞幅,如果長寬都等于r就是正方形,不一樣就是矩形量瓜;通過這些數(shù)據(jù)就可以得到斜邊距離司恳,這個距離就是距離中心點的距離,然后我們再固定距離的方向绍傲,只需要四個點的方向就可以同過下面的方法計算出矩形四個頂點的經(jīng)緯數(shù)據(jù)扔傅,便可以通過地圖的接口來繪制矩形蒜撮。
代碼給大家分享一下
////? CaculateLL.m//? iLe////? Created by Jany on 17/5/22.//? Copyright ? 2017年 apple. All rights reserved.//#import "CaculateLL.h"#include#define KmPerDegree? ? 111.12000071117
#define DegreesPerKm? ? (1.0/KmPerDegree)
#define PI? ? ? ? ? ? ? M_PI
#define TwoPI? ? ? ? ? (M_PI+M_PI)
#define HalfPI? ? ? ? ? M_PI_2
#define RadiansPerDegree? ? (PI/180.0)
#define DegreesPerRadian? ? (180.0/PI)
#define copysign(x,y)? ? ? (((y)<0.0)?-fabs(x):fabs(x))
#define NGT1(x)? ? (fabs(x)>1.0?copysign(1.0,x):(x))
//#define ArcCos(x) (fabs(x)>1?quiet_nan():acos(x)) //Hack
#define ArcCos(x)? (acos(x))
#define hav(x)? ? ? ((1.0-cos(x))*0.5)? ? ? /* haversine */
#define ahav(x)? ? (ArcCos(NGT1(1.0-((x)*2.0))))? /* arc haversine */
#define sec(x)? ? ? (1.0/cos(x))? ? ? ? ? ? /* secant */
#define csc(x)? ? ? (1.0/sin(x))? ? ? ? ? ? /* cosecant */
@implementation CaculateLL
static CaculateLL *caculateLLManager = nil;
+(id)shareInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
caculateLLManager = [[CaculateLL alloc]init];
});
return caculateLLManager;
}
-(CLLocationCoordinate2D)moveLocation:(CLLocationCoordinate2D)startLocation movementInMeters:(double)movementInMeters movementBearing:(double)movementBearing
{
NSLog(@"------%f----%f",movementBearing,movementInMeters);
double? dist = (movementInMeters / 1000);? /* -> great-circle distance (km) */
double? course = movementBearing;? ? ? ? ? /* -> initial great-circle course (degrees) */
double? slt = startLocation.latitude;? ? /* -> starting decimal latitude (-S) */
double? slg = startLocation.longitude;? /* -> starting decimal longitude(-W) */
double? xlt = 0;? ? /* <- ending decimal latitude (-S) */
double? xlg = 0;? ? /* <- ending decimal longitude(-W) */
double? c, d, dLo, L1, L2, coL1, coL2, l;
if (dist > KmPerDegree*180.0) {
course -= 180.0;
if (course < 0.0) course += 360.0;
dist? ? = KmPerDegree*360.0-dist;
}
if (course > 180.0) course -= 360.0;
c? ? = course*RadiansPerDegree;
d? ? = dist*DegreesPerKm*RadiansPerDegree;
L1? = slt*RadiansPerDegree;
slg *= RadiansPerDegree;
coL1 = (90.0-slt)*RadiansPerDegree;
coL2 = ahav(hav(c)/(sec(L1)*csc(d))+hav(d-coL1));
L2? = HalfPI-coL2;
l? ? = L2-L1;
if ((dLo=(cos(L1)*cos(L2))) != 0.0)
dLo? = ahav((hav(d)-hav(l))/dLo);
if (c < 0.0) dLo = -dLo;
slg += dLo;
if (slg < -PI)
slg += TwoPI;
else if (slg > PI)
slg -= TwoPI;
xlt = L2*DegreesPerRadian;
xlg = slg*DegreesPerRadian;
CLLocationCoordinate2D? ll = CLLocationCoordinate2DMake(xlt, xlg);
return ll;
}
@end