Note that there are some explanatory texts on larger screens.

plurals
  1. POMKMapView Delegate Methods not working
    text
    copied!<p>I just wanted to add a Polyline to my Map which is displayed in a tableviewcell. Unfortunately the delegate methods are not called... Would be nice if someone knows why.</p> <p>My tableview.h:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "Route.h" #import &lt;MapKit/MapKit.h&gt; #import &lt;QuartzCore/QuartzCore.h&gt; @interface RoutesDetailView : UITableViewController&lt;MKMapViewDelegate&gt;{ Route *myRoute; MKMapView *mapView; // the view we create for the line on the map MKPolylineView* _routeLineView; // the rect that bounds the loaded points MKMapRect _routeRect; MKPolyline* _routeLine; } @property (nonatomic, retain) Route *myRoute; @property (nonatomic,retain) MKMapView *mapView; @property (nonatomic, retain) MKPolyline* routeLine; @property (nonatomic, retain) MKPolylineView* routeLineView; -(MKPolyline *) loadRoute: (Route *) theRoute; @end </code></pre> <p>And my tableview.m:</p> <pre><code> @implementation RoutesDetailView @synthesize myRoute,mapView; @synthesize routeLine = _routeLine; @synthesize routeLineView = _routeLineView; - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; MKMapView *myMap = [[MKMapView alloc] initWithFrame:CGRectMake(10, 1, 300 , 300)]; myMap.layer.cornerRadius = 10.0; [self setMapView:myMap]; [mapView setDelegate:self]; CLLocationCoordinate2D annotationCoord; annotationCoord.latitude = [[NSString stringWithFormat:@"%@",NSLocalizedString(@"DefaultPointLAT", nil)] doubleValue]; annotationCoord.longitude = [[NSString stringWithFormat:@"%@",NSLocalizedString(@"DefaultPointLONG", nil)] doubleValue]; MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; annotationPoint.coordinate = annotationCoord; MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (annotationPoint.coordinate,[[NSString stringWithFormat:@"%@",NSLocalizedString(@"DefaultCircle", nil)] doubleValue], [[NSString stringWithFormat:@"%@",NSLocalizedString(@"DefaultCircle", nil)] doubleValue]); [mapView setRegion:region animated:NO]; // Uncomment the following line to preserve selection between presentations. // self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } #pragma mark - Table view data source - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { . . . static NSString *CellIdentifier = @"CellMap"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; [mapView setFrame:CGRectMake(10, 1, cell.frame.size.width-20 , cell.frame.size.height-1)]; [cell addSubview:mapView]; [mapView addOverlay:[self loadRoute:myRoute]]; return cell; . . . } #pragma mark - Table view delegate -(MKPolyline *) loadRoute: (Route *) theRoute { MKMapPoint northEastPoint; MKMapPoint southWestPoint; // create a c array of points. MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * theRoute.latitude.count); for(int idx = 0; idx &lt; theRoute.latitude.count; idx++) { CLLocationDegrees latitude = [[[theRoute latitude] objectAtIndex:idx] doubleValue]; CLLocationDegrees longitude = [[[theRoute longitude] objectAtIndex:idx] doubleValue]; // create our coordinate and add it to the correct spot in the array CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude); MKMapPoint point = MKMapPointForCoordinate(coordinate); // // adjust the bounding box // // if it is the first point, just use them, since we have nothing to compare to yet. if (idx == 0) { northEastPoint = point; southWestPoint = point; } else { if (point.x &gt; northEastPoint.x) northEastPoint.x = point.x; if(point.y &gt; northEastPoint.y) northEastPoint.y = point.y; if (point.x &lt; southWestPoint.x) southWestPoint.x = point.x; if (point.y &lt; southWestPoint.y) southWestPoint.y = point.y; } pointArr[idx] = point; } // create the polyline based on the array of points. self.routeLine = [MKPolyline polylineWithPoints:pointArr count:theRoute.latitude.count]; _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y); // clear the memory allocated earlier for the points free(pointArr); return self.routeLine; } - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay { NSLog(@"DELEGATE CALL"); MKOverlayView* overlayView = nil; if(overlay == self.routeLine) { //if we have not yet created an overlay view for this overlay, create it now. if(nil == self.routeLineView) { self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine]; self.routeLineView.fillColor = [UIColor redColor]; self.routeLineView.strokeColor = [UIColor redColor]; self.routeLineView.lineWidth = 15; } overlayView = self.routeLineView; } return overlayView; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic may go here. Create and push another view controller. /* &lt;#DetailViewController#&gt; *detailViewController = [[&lt;#DetailViewController#&gt; alloc] initWithNibName:@"&lt;#Nib name#&gt;" bundle:nil]; // ... // Pass the selected object to the new view controller. [self.navigationController pushViewController:detailViewController animated:YES]; */ } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload