Ios 自定义覆盖,半径以米为单位
我通过创建一个符合MKOverlay协议的NSObject子类和一个MKOverlayPathRenderer子类来创建一个自定义覆盖。我的目标是在MKMapView上创建一个锚定到用户位置的圆形覆盖,我的工作很好。无论何时设置覆盖上的坐标,渲染器都会使用键值观察使其绘制的路径无效,然后重新绘制 我遇到的问题是,我希望圆的半径以米为单位,但我认为我的计算不对,或者我遗漏了什么。我在下面发布了覆盖对象和渲染器的源代码(渲染器的接口中没有任何内容)。举个例子,我把半径设置为200米,但在地图视图中,它只显示为大约10米。有人知道怎么解决吗Ios 自定义覆盖,半径以米为单位,ios,mkmapview,mkoverlay,mkoverlaypathrenderer,Ios,Mkmapview,Mkoverlay,Mkoverlaypathrenderer,我通过创建一个符合MKOverlay协议的NSObject子类和一个MKOverlayPathRenderer子类来创建一个自定义覆盖。我的目标是在MKMapView上创建一个锚定到用户位置的圆形覆盖,我的工作很好。无论何时设置覆盖上的坐标,渲染器都会使用键值观察使其绘制的路径无效,然后重新绘制 我遇到的问题是,我希望圆的半径以米为单位,但我认为我的计算不对,或者我遗漏了什么。我在下面发布了覆盖对象和渲染器的源代码(渲染器的接口中没有任何内容)。举个例子,我把半径设置为200米,但在地图视图中,
//Custom Overlay Object Interface
@import Foundation;
@import MapKit;
@interface CustomRadiusOverlay : NSObject <MKOverlay>
+ (id)overlayWithCoordinate:(CLLocationCoordinate2D)coordinate radius:(CLLocationDistance)radius;
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic) MKMapRect boundingMapRect;
@property (nonatomic) CLLocationDistance radius;
@end
//Custom overlay
#import "CustomRadiusOverlay.h"
@implementation LFTRadiusOverlay
+ (id)overlayWithCoordinate:(CLLocationCoordinate2D)coordinate radius:(CLLocationDistance)radius{
CustomRadiusOverlay* overlay = [LFTRadiusOverlay new];
overlay.coordinate = coordinate;
overlay.radius = radius;
return overlay;
}
- (MKMapRect)boundingMapRect{
MKMapPoint upperLeft = MKMapPointForCoordinate(self.coordinate);
MKMapRect bounds = MKMapRectMake(upperLeft.x, upperLeft.y, self.radius*2, self.radius*2);
return bounds;
}
- (void)setCoordinate:(CLLocationCoordinate2D)coordinate{
_coordinate = coordinate;
self.boundingMapRect = self.boundingMapRect;
}
@end
#import "CustomOverlayRadiusRenderer.h"
#import "CustomRadiusOverlay.h"
@interface CustomOverlayRadiusRenderer()
@property (nonatomic) CustomRadiusOverlay* circleOverlay;
@end
@implementation CustomOverlayRadiusRenderer
- (id)initWithOverlay:(id<MKOverlay>)overlay{
self = [super initWithOverlay:overlay];
if(self){
_circleOverlay = (LFTRadiusOverlay*)overlay;
[_circleOverlay addObserver:self forKeyPath:@"coordinate" options:NSKeyValueObservingOptionNew context:NULL];
self.fillColor = [UIColor redColor];
self.alpha = .7f;
}
return self;
}
- (void)createPath{
CGMutablePathRef path = CGPathCreateMutable();
MKMapPoint mapPoint = MKMapPointForCoordinate(self.circleOverlay.coordinate);
CGPoint point = [self pointForMapPoint:mapPoint];
CGPathAddArc(path, NULL, point.x, point.y, self.circleOverlay.radius, 0, kDegreesToRadians(360), YES);
self.path = path;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
[self invalidatePath];
}
@end
//自定义覆盖对象接口
@进口基金会;
@导入地图套件;
@接口CustomRadiusOverlay:NSObject
+(id)带坐标的叠加:(CLLocationCoordinate2D)坐标半径:(CLLocationDistance)半径;
@性质(非原子)CLLocationCoordinate2D坐标;
@属性(非原子)MKMapRect boundingMapRect;
@性质(非原子)CLLocationDistance半径;
@结束
//自定义覆盖
#导入“CustomRadiusOverlay.h”
@实现LFTRadiusOverlay
+(id)带坐标的叠加:(CLLocationCoordinate2D)坐标半径:(CLLocationDistance)半径{
CustomRadiusOverlay*overlay=[LFTRadiusOverlay new];
overlay.coordinate=坐标;
叠加半径=半径;
返回叠加;
}
-(MKMapRect)boundingMapRect{
MKMapPoint upperLeft=MKMapPointForCoordinate(self.coordinate);
MKMapRect bounds=MKMapRectMake(左上角.x,左上角.y,自半径*2,自半径*2);
返回边界;
}
-(无效)设置坐标:(CLLocationCoordinate2D)坐标{
_坐标=坐标;
self.boundingMapRect=self.boundingMapRect;
}
@结束
#导入“CustomOverlayRadiusRenderer.h”
#导入“CustomRadiusOverlay.h”
@接口CustomOverlayRadiusRenderer()
@属性(非原子)CustomRadiusOverlay*circleOverlay;
@结束
@实现CustomOverlayRadiusRenderer
-(id)initWithOverlay:(id)overlay{
self=[super initWithOverlay:overlay];
如果(自我){
_圆形覆盖层=(LFTRadiusOverlay*)覆盖层;
[_CircleOverlayAddObserver:self-forKeyPath:@“坐标”选项:NSKeyValueObservingOptionNew-context:NULL];
self.fillColor=[UIColor redColor];
self.alpha=.7f;
}
回归自我;
}
-(void)创建路径{
CGMutablePathRef path=CGPathCreateMutable();
MKMapPoint mapPoint=MKMapPointForCoordinate(self.circleOverlay.coordinate);
CGPoint point=[self pointForMapPoint:mapPoint];
CGPathAddArc(路径,空,点.x,点.y,自圆覆盖半径,0,kDegreesToRadians(360),是);
self.path=路径;
}
-(void)observeValueForKeyPath:(NSString*)对象的键路径:(id)对象更改:(NSDictionary*)更改上下文:(void*)上下文{
[自我失效路径];
}
@结束
您可以绘制米(作为半径),但需要在MapPoints中指定所有内容
因此,转换单位:
~~~
mapPoints=meters*mkmappointspermeteratitude(坐标.纬度)
你是上帝。工作完美,甚至不知道该功能的存在。