用米为单位的半径创建自定义的MKOverlay

4
我通过创建一个NSObject子类并符合MKOverlay协议以及一个MKOverlayPathRenderer的子类来制作了自定义覆盖层。我的目标是创建一个圆形覆盖层,将其锚定到MKMapView上的用户位置,并且我已经成功实现了这一点。每当我的覆盖层的坐标被设置时,使用键值观察的渲染器会使其绘制的路径无效化,然后重新绘制。
但是我遇到的问题是,我希望圆的半径是以米为单位的,但我认为我的数学计算有误或者我漏掉了什么东西。我在下面发布了覆盖对象和渲染器的源代码(渲染器的接口中没有任何内容)。例如,我将半径设置为200米,但在地图视图上,它只显示大约10米。有人知道如何解决这个问题吗?
//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
1个回答

5

您画的是以半径为单位的米,但需要使用MapPoints来指定所有内容。

因此,需要转换单位:

~~ mapPoints = meters * MKMapPointsPerMeterAtLatitude(coordinate.latitude)


你真是太厉害了。完美地运行,我甚至不知道那个函数的存在。 - barndog

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接