简体   繁体   English

无法在地图上显示MKPolyline

[英]Cannot display MKPolyline on the map

I'm fighting the hell with theses freaking polylines… 我正在用折断折线的方法来战斗……

I still can't display it on my map ! 我仍然无法在地图上显示它!

I implemented : 我实现了:

NSArray *points = [NSArray arrayWithObjects:
                   [[CLLocation alloc] initWithLatitude:45.43894 longitude:-73.7396],
                   [[CLLocation alloc] initWithLatitude:45.44073 longitude:-73.73998],
                   // 72 more like that…
                   nil];

( Note that theses points are thoses used in the NVPolyline depository on github . First I tried to use theses classes but I couldn't display the polylines either… ) (请注意,这些点是在github上的NVPolyline存储库中使用的那些点。首先,我尝试使用这些类,但也无法显示折线…)

Then I do (like here ): 然后我做(就像这里 ):

int numPoints = [points count];
CLLocationCoordinate2D *arrayPtr = malloc(numPoints * sizeof(CLLocationCoordinate2D));

for(int i = 0; i<numPoints; i++) {
    arrayPtr[i] = [[points objectAtIndex:i] coordinate];
}

polyline = [MKPolyline polylineWithCoordinates:arrayPtr count:numPoints];

[map addOverlay:polyline];

Instead of adding the polyline overlay, I also tried : 我也没有尝试添加折线叠加层:

MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:polyline];
[map addSubview:polylineView];

A priori, it should work because MKPolylineView inherit from UIView , but it crashes. 先验的,它应该起作用,因为MKPolylineView继承自UIView ,但是它崩溃了。

Here is the log : 这是日志:

-[CALayer levelsOfDetail]: unrecognized selector sent to instance 0x1d8319e0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayer levelsOfDetail]: unrecognized selector sent to instance 0x1d8319e0'

I have set my delegate through the storyboard, but I also tried to add [map setDelegate:self]; 我已经通过情节[map setDelegate:self];设置了我的代表,但是我也尝试添加[map setDelegate:self]; but nothing changes. 但没有任何变化。

Where and why I'm wrong ? 我在哪里以及为什么错了?

Thanks for help and ideas. 感谢您的帮助和想法。

EDIT, added : 编辑,添加:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay >)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]])
    {
         MKOverlayView *pView = [[MKOverlayView alloc] initWithOverlay:overlay];
         return pView;
    }

    return nil;
}

Still doesn't work, I shall be idiot. 仍然不起作用,我将是个白痴。

I also show you my .h : (seems correct for me) 我也向您展示我的.h :(似乎对我来说是正确的)

@interface MapViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate>
{
    MKPolyline *polyline;
}

@property (weak, nonatomic) IBOutlet MKMapView *map;

@end

Haha what a joke ! 哈哈开个玩笑!

In fact, the lineWidth of MKOverlayPathView is set by default to 0… 实际上, MKOverlayPathViewlineWidth默认设置为0…

( Check the MKOverlayPathView class reference ) (检查MKOverlayPathView类参考

First I thought it was the thing but it's not… the important thing is to have a strokeColor set. 首先,我认为这是问题,但事实并非如此…… 重要的是设置了strokeColor

And because my [map addSubview:polylineView]; 并且因为我的[map addSubview:polylineView]; makes my app crash (and I don't get why), I have to use [map addOverlay:polyline]; 使我的应用崩溃(我不知道为什么),我必须使用[map addOverlay:polyline]; instead. 代替。

So I added : 所以我补充说:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView *thePolylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    thePolylineView.strokeColor = [UIColor purpleColor]; // <-- So important stuff here
    thePolylineView.lineWidth = 5.0;
    return thePolylineView;
}

Thanks for your help @Kirualex :). 感谢您的帮助@Kirualex :)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM