【问题标题】:MKMapView Delegate Methods not workingMKMapView 委托方法不起作用
【发布时间】:2012-05-03 16:35:13
【问题描述】:

我只是想在我的地图中添加一条折线,它显示在 tableviewcell 中。很遗憾 不调用委托方法...如果有人知道为什么会很好。

我的 tableview.h:

#import <UIKit/UIKit.h>
#import "Route.h"
#import <MapKit/MapKit.h>
#import <QuartzCore/QuartzCore.h>

@interface RoutesDetailView : UITableViewController<MKMapViewDelegate>{
    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

还有我的 tableview.m:

    @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 < 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 > northEastPoint.x)
                    northEastPoint.x = point.x;
                if(point.y > northEastPoint.y)
                    northEastPoint.y = point.y;
                if (point.x < southWestPoint.x)
                    southWestPoint.x = point.x;
                if (point.y < 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.
        /*
         <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
         // ...
         // Pass the selected object to the new view controller.
         [self.navigationController pushViewController:detailViewController animated:YES];
         */
    }

    @end

【问题讨论】:

  • 你的意思是 viewForOverlay 没有被调用?记录添加的每个点的纬度和经度,并确保它们正确(未交换、在范围内等)。此外,在 malloc 中,您会得到 sizeof CLLocationCoordinate2D 但数组包含 MKMapPoint 结构。这两个结构不是一回事,但它们恰好具有相同的大小,因此您看不到任何问题。
  • 是的,viewForOverlay 没有被调用,lat 和 long 是正确的。此委托方法在另一个视图控制器中完美运行......但在 uitableview 单元格中不起作用

标签: ios uitableview delegates mkmapview


【解决方案1】:

试试这个,我遇到了同样的问题。在尝试了许多组合之后,这是一个有效的组合。

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController<MKMapViewDelegate> {

    MKMapView   *mapView;
}

以及实现...

- (void)viewDidLoad {

    [super viewDidLoad];

    mapView = [[MKMapView alloc] initWithFrame: CGRectMakeFullScreenIphone];
    mapView.delegate = self;
    [mapView setMapType: MKMapTypeStandard];
    [self.view addSubview: mapView]; 


    MKCoordinateRegion newRegion;
    // configure region...
    [mapView setRegion:newRegion animated:YES];

    CLLocationCoordinate2D coordinate;
    //configure coordinate...

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
    [annotation setCoordinate:coordinate];
    [annotation setTitle:@"TEST"];
    [mapView addAnnotation:annotation];
}

上面的简单代码运行良好,并且调用了委托的方法。

【讨论】:

    【解决方案2】:

    如果你在模拟器中运行应用程序,那么

    - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay
    

    此委托方法不会被调用,您需要在 iOS 设备上运行它。

    【讨论】:

      【解决方案3】:

      第一个方法是

      ma​​pView:regionDidChangeAnimated:

      第二个是

      ma​​pView:didUpdateUserLocation:

      头文件

      #import <UIKit/UIKit.h>
      #import <MapKit/MapKit.h>
      
      @interface ViewController : UIViewController <MKMapViewDelegate>
      @property (weak, nonatomic) IBOutlet MKMapView *mapView;
      @property (weak, nonatomic) IBOutlet UIButton *searchButton;
      
      @end
      

      实施文件

      @interface ViewController ()
      
      @end
      
      @implementation ViewController
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          self.mapView.delegate = self;
          self.mapView.mapType = MKMapTypeStandard;
          self.mapView.showsUserLocation = YES;
          self.searchButton.hidden = YES;
      }
      
      - (IBAction)setMapType:(UISegmentedControl *)sender {
          switch (sender.selectedSegmentIndex) {
              case 0:
                  self.mapView.mapType = MKMapTypeStandard;
                  break;
              case 1:
                  self.mapView.mapType = MKMapTypeSatellite;
                  break;
              case 2:
                  self.mapView.mapType = MKMapTypeHybrid;
                  break;
              default:
                  break;
          }
      }
      
      - (IBAction)zoomToCurrentLocation:(UIBarButtonItem *)sender {
          float spanX = 0.00725;
          float spanY = 0.00725;
          MKCoordinateRegion region;
          region.center.latitude = self.mapView.userLocation.coordinate.latitude;
          region.center.longitude = self.mapView.userLocation.coordinate.longitude;
          region.span.latitudeDelta = spanX;
          region.span.longitudeDelta = spanY;
          self.searchButton.hidden = YES;
          [self.mapView setRegion:region animated:YES];
      }
      
      -(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
          self.searchButton.hidden = NO;
      }
      
      -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
          [self.mapView setCenterCoordinate:userLocation.coordinate animated:YES];
      }
      
      - (void)didReceiveMemoryWarning
      {
          [super didReceiveMemoryWarning];
          // Dispose of any resources that can be recreated.
      }
      
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多