Question

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… )

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 MKPolylineViewinherit from UIView, but it crashes.

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]; 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)

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

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

@end
Was it helpful?

Solution

Haha what a joke !

In fact, the lineWidth of MKOverlayPathView is set by default to 0…

( Check the MKOverlayPathView class reference )

First I thought it was the thing but it's not… the important thing is to have a strokeColor set.

And because my [map addSubview:polylineView]; makes my app crash (and I don't get why), I have to use [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 :).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top