如何使用叠加层将UIBezierPath添加到MKMapView [英] How to add a UIBezierPath to a MKMapView using overlays

查看:81
本文介绍了如何使用叠加层将UIBezierPath添加到MKMapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用自定义MKOverlayPathView子类和一个向确认的自定义类向地图视图添加半圈,但是我什么也没能显示在地图视图上.我使用UIBezierPath创建圆的路径,但是无论我尝试什么,我都看不到我的路径... 有没有人有将UIBezierPath用作覆盖的示例程序?

I 've been trying to add a half circle to a map view using a custom MKOverlayPathView subclass and a custom class which confirms to but I cannot get anything to show up on the map view. I use a UIBezierPath to create the path for the circle, but no matter what I try, I cannot see my path... Does anyone have an example program in which a UIBezierPath is being used as overlay?

谢谢!

蒂姆

回答安娜的问题:

我可以毫无问题地绘制MKCircleViews,但是我似乎为MKOVerlay(Path)View的自定义子类苦苦挣扎.设置了委托,并通过NSLog确认确实添加了我的叠加层,仅缺少View部分...

I can draw MKCircleViews without any problem, but I seem to struggle with custom subclasses for MKOVerlay(Path)View. The delegate is set, and an NSLog confirms that my overlay is actually added, only the View part is missing ...

在我的覆盖类的头文件下面.注释和变量使用荷兰语,如果有人想要翻译,我可以提供,但我认为一切都应该清楚.我创建了一个具有中心坐标和半径的叠加层.

Below the header file for my overlay class. Comments and variables are in Dutch, if anyone wants a translation I can provide it but I think everything should be clear. I create a overlay with centre coordinate and radius.

boundingMapRect是从中心坐标计算得出的.原点是中心减去x和y的半径,而宽度+高度是半径的两倍.

The boundingMapRect is calculated from the centre coordinate. Origin is centre minus the radius for both x and y, and the width + height are twice the radius.

@interface PZRMijnOverlay : NSObject <MKOverlay>
{

}
//een property die een bezierpath beschrijft, naar de 2 standaard overlay properties
@property (nonatomic, strong) UIBezierPath *bezierPath;
@property (nonatomic) CLLocationDistance straal;

//class method
+(PZRMijnOverlay *)bezierCirkelMetCenterCoordinate: (CLLocationCoordinate2D)coordinaat enStraal: (CLLocationDistance)eenStraal;

//een eigen designated init
-(id)initWithCenterCoordinate: (CLLocationCoordinate2D)coordinaat enStraal: (CLLocationDistance)eenStraal;

@end

这是viewForOverlay方法的实现:

This is de viewForOverlay method implementation:

PZROverlayView *overlayView = [[PZROverlayView alloc] initWithOverlay:(PZRMijnOverlay *)overlay];

overlayView.strokeColor = [UIColor redColor];
overlayView.lineWidth = 10;
//overlayView.strokeColor = [UIColor blackColor];

return overlayView;

现在我想我应该覆盖我的PZROverlayView类中的createPath方法,但是我无法弄清楚需要去那里的代码.我试图创建一个UIBezierPath,将其转换为CGPath并将其分配给MKOverlayPathView超类的path属性.

Now I guess I should overwrite the createPath method in my PZROverlayView class, but I cannot figure out what code needs to go there. What I tried was to create a UIBezierPath, convert it to CGPath and assign it to the path property of the MKOverlayPathView super class.

推荐答案

好的,我终于发现我做错了!实际上,这比我想象的要容易得多!

Alright, I finally found what I was doing wrong! It is actually way easier than I thought!

解决方案:

  1. 添加MKCircle叠加层
  2. 在viewForOverlay中:创建一个bezierPath(CG或UI)并将其添加到标准MKOverlayPathView实例的path属性中

我通过使用自定义类使自己变得太困难了...

I was making it myself way too difficult by using custom classes...

这篇关于如何使用叠加层将UIBezierPath添加到MKMapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆