IOS

一个经纬度绘制矩形

2017-06-12  本文已影响149人  Jany_4a9a

     之前一直做的是圆形电子围栏,相对比较简单,因为第三方地图都有借口可以绘制;但第三方地图绘制多边形都需要每个拐角点都要经纬度,这个跟我现在的项目不符合;因为我只能得到一个经纬点。

因为电子围栏的范围是我们设置的,如果长宽都等于r就是正方形,不一样就是矩形;通过这些数据就可以得到斜边距离,这个距离就是距离中心点的距离,然后我们再固定距离的方向,只需要四个点的方向就可以同过下面的方法计算出矩形四个顶点的经纬数据,便可以通过地图的接口来绘制矩形。

下面是效果

代码给大家分享一下

////  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

上一篇 下一篇

猜你喜欢

热点阅读