I'm pretty new on programmin to develop iphone applications and i would like to know why the MKPolyline that I create with 2 MKMapPoints doesn't appears in the MKMapView that I insert on my view. Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
map = [[MKMapView alloc] initWithFrame:self.view.bounds];
MKMapPoint * pointsArray = malloc(sizeof(CLLocationCoordinate2D)*4);
CLLocationCoordinate2D punto1;
punto1.latitude =39.468502;
punto1.longitude =-0.398469;
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init];
annotationPoint.coordinate = punto1;
annotationPoint.title = @"Point 1";
MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc]init];
annotationPoint2.coordinate = CLLocationCoordinate2DMake(39.472312,-0.386453);
annotationPoint2.title = @"Point 2";
[map addAnnotation:annotationPoint];
[map addAnnotation:annotationPoint2];
pointsArray[0]= MKMapPointForCoordinate(punto1);
pointsArray[1]= MKMapPointForCoordinate(CLLocationCoordinate2DMake(39.467011,-0.390015));
pointsArray[2]= MKMapPointForCoordinate(CLLocationCoordinate2DMake(39.469926,-0.392118));
pointsArray[3]= MKMapPointForCoordinate(CLLocationCoordinate2DMake(39.472312,-0.386453));
routeLine = [MKPolyline polylineWithPoints:pointsArray count:4];
free(pointsArray);
[map addOverlay:routeLine];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(39.467011,-0.392515), 1100, 1100);
[map setRegion:region];
[self.view insertSubview:map atIndex:0];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay
{
MKOverlayView* overlayView = nil;
MKPolylineView * routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
routeLineView.fillColor = [UIColor blueColor];
routeLineView.strokeColor = [UIColor orangeColor];
routeLineView.lineWidth = 3;
overlayView = routeLineView;
return overlayView;
}
The annotations are OK and they show correctly on the map. Hope someone can help, thanks!!!
The MKPolyline
doesn't show because the map's delegate
is not set.
The viewForOverlay
delegate method won't get called if the map's delegate
property is not set. Because the delegate method is never called, the MKPolylineView
never gets created, etc...
After creating the MKMapView
, set its delegate
:
map = [[MKMapView alloc] initWithFrame:self.view.bounds];
map.delegate = self; // <-- add this
I'd like to mention a few other unrelated points:
Since you are putting MKMapPoint
values in the pointsArray
, the malloc
should use sizeof(MKMapPoint)
instead of sizeof(CLLocationCoordinate2D)
. It happens to work with the wrong code because both structs happen to be the same size. But you should still use the right code.
MKPolyline
also has a polylineWithCoordinates:count:
method so you can pass CLLocationCoordinate2D
instead of MKMapPoint
structs. This can be easier to read, understand, and avoids having to convert from coordinates to mappoints.
The MKPolylineView
uses strokeColor
only so setting the fillColor
for it does nothing.
In the viewForOverlay
delegate method, it's much better practice to use the overlay
parameter that is passed into the method instead of the externally declared routeLine
. This will be extremely important if you want to add more than one overlay. You would also first check what kind of class overlay
is and then create the appropriate view for it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.