我想用箭头方向绘制折线.据我所知,Mapkit中尚未包含这些内容.
我试图创建一个自定义的MKOverlayPathRenderer并覆盖drawMapRect但我对如何执行此操作有点困惑.
有没有办法实现我的目标?
这是我正在尝试做的一个例子
我在地图上有很多长折线.我想优化他们的绘图,因为在几千点处,折线被绘制得非常慢.
我drawMapRect看起来像这样:
- for each polyline segment
- verify if it's bounding box intersects the currently drawn MKMapRect
- if id does, draw it
Run Code Online (Sandbox Code Playgroud)
如果没有太多分数,哪个很好.但是当有8-16个可见的地图和2-3000个点时,它们的速度非常慢for.
如果它们只是位置,那么解决方案就是实现某种四叉树/ r树结构,只过滤当前绘制的那些位置MKMapRect,但我不确定这是否适合折线本身.
如果我仅过滤当前maprect内的段端点,则可能不会绘制某些线段.例如,1-2点之间的两个红色maprects中没有段端点,但仍然需要绘制...
是否有某种算法类似于四叉树或某种方法来解决这个问题?
我通过创建一个符合MKOverlay协议和MKOverlayPathRenderer子类的NSObject子类来进行自定义覆盖.我的目标是在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 …Run Code Online (Sandbox Code Playgroud) 我将两个不同的MKGeodesicPolyline实例添加到MKMapView这样的
CLLocation *LAX = [[CLLocation alloc] ...];
CLLocation *JFK = [[CLLocation alloc] ...];
CLLocation *LHR = [[CLLocation alloc] ...];
CLLocationCoordinate2D laxToJfkCoords[2] = {LAX.coordinate, JFK.coordinate};
CLLocationCoordinate2D jfkToLhrCoords[2] = {JFK.coordinate, LHR.coordinate};
MKGeodesicPolyline *laxToJfk = [MKGeodesicPolyline polylineWithCoordinates:laxToJfkCoords count:2];
MKGeodesicPolyline *jfkToLhr = [MKGeodesicPolyline polylineWithCoordinates:jfkToLhrCoords count:2];
[mapView addOverlay:laxToJfk];
[mapView addOverlay:jfkToLhr];
Run Code Online (Sandbox Code Playgroud)
我想用需要在rendererForOverlay委托方法中配置的不同样式来渲染这两个叠加层。
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay {
if (![overlay isKindOfClass:[MKPolyline class]]) {
return nil;
}
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:(MKPolyline *)overlay];
renderer.lineWidth = 3.0f;
// How to …Run Code Online (Sandbox Code Playgroud)