首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义半径以米为单位的MKOverlay

自定义半径以米为单位的MKOverlay
EN

Stack Overflow用户
提问于 2013-12-25 01:14:28
回答 1查看 1.7K关注 0票数 4

我通过创建一个符合NSObject协议的MKOverlay子类和一个MKOverlayPathRenderer子类来创建一个自定义覆盖。我的目标是创建一个循环覆盖,它锚定在MKMapView上的用户位置上,而且我的工作非常好。每当设置了覆盖上的坐标时,我的渲染器就会使用键值观察,使它所绘制的路径无效,然后重新绘制。

我遇到的问题是,我希望圆的半径以米为单位,但我不认为我在做正确的数学运算,或者我遗漏了一些东西。我在下面发布了覆盖对象和呈现器的源代码(渲染器的接口中没有任何内容)。举个例子,我把半径设定在200米,但在地图上,它只显示为10米左右。有人知道怎么解决吗?

代码语言:javascript
复制
//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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-25 01:40:19

您可以绘制米(作为半径),但需要在MapPoints中指定所有内容。

因此,转换单位:

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20768261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档