0

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
Community
  • 1
  • 1
Lucien
  • 451
  • 4
  • 16
  • Do you have the crash log? – Alexis C. May 16 '13 at 13:25
  • Did you look into Apple's [breadcrumb example](http://developer.apple.com/library/ios/#samplecode/Breadcrumb/Listings/ReadMe_txt.html#//apple_ref/doc/uid/DTS40010048-ReadMe_txt-DontLinkElementID_11)? It is used to trace users path on the mapview. You can modify it to your needs. – Amar May 16 '13 at 13:36
  • Yes I already looked this but I just got the blue dot and nothing more. I just can access the info button. And the app do nothing more. Weird. – Lucien May 16 '13 at 13:44
  • Do you implement the `viewForOverlay` method correctly ? Could we have a look at it? – Alexis C. May 16 '13 at 14:05
  • Once you add a polyline to your `MKMapView`, the map instance call its delegate (your viewController) to ask how to display this polyline. See http://stackoverflow.com/questions/10598322/iphone-how-to-draw-line-between-two-points-on-mapkit – Alexis C. May 16 '13 at 14:10
  • Yes but this `MKMapView`shall have a default way to display it no ? – Lucien May 16 '13 at 14:21
  • not that I know of, you have to create the actual view like this for instance : `self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease]; self.routeLineView.fillColor = [UIColor redColor]; self.routeLineView.strokeColor = [UIColor redColor]; self.routeLineView.lineWidth = 3;` – Alexis C. May 16 '13 at 14:25
  • It changes nothing. :/ – Lucien May 16 '13 at 14:31
  • You should try logging every step and make sure your delegate is set because your code seems good. You can also put more of your code so we can compile on our end to check it out. – Alexis C. May 16 '13 at 15:32

1 Answers1

0

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 :).

Lucien
  • 451
  • 4
  • 16