viewForOverlay没有被调用

时间:2012-06-01 19:51:00

标签: iphone objective-c xcode ios5

我想在ios的地图上绘制两条路径之间的路线。问题是没有调用mapView:viewforoverlay。

这是我的代码完整代码。解析没有问题,因为我已经使用NSLog语句对其进行了测试。我已从此处自定义代码:http://spitzkoff.com/craig/?p=136

感谢这个网站。请帮帮我。

    #import "AppViewController.h"
    #import "XMLParser.h"
    #import "AppAppDelegate.h"

     @interface AppViewController ()

      @end

      @implementation AppViewController

      @synthesize mapview= _mapview;
      @synthesize routeLine = _routeLine;
      @synthesize routeLineView = _routeLineView;

      @synthesize coordinatearray = _coordinatearray;
      @synthesize pathlatandlong = _pathlatandlong;
      @synthesize coordinate = _coordinate;


      - (void)viewDidLoad
      {
          [super viewDidLoad];
          [self doParse];

        // Do any additional setup after loading the view, typically from a nib.
      }

      - (void)viewDidUnload
      {
          [self setMapview:nil];
          [super viewDidUnload];
          // Release any retained subviews of the main view.
      }          

      - (BOOL)shouldAutorotateToInterfaceOrientation:                              (UIInterfaceOrientation)interfaceOrientation
     {
         return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
     }

     -(void)doParse
     {
         [_coordinatearray removeAllObjects];

         NSURL *parseurl = [[NSURL alloc] initWithString:@"https://maps.google.com/maps?f=d&hl=en&saddr=19.055229,72.830829&daddr=19.113611,72.871389&ie=UTF8&om=0&output=kml"];

         NSXMLParser *nsxmlparser = [[NSXMLParser alloc] initWithContentsOfURL:parseurl];

         XMLParser *parser = [[XMLParser alloc] initXMLParser];

         [nsxmlparser setDelegate:parser];


         BOOL success = [nsxmlparser parse];

         if(success)
         {

             [self loadRoute];

             if (nil != self.routeLine) 
             {
                 [self.mapview addOverlay:self.routeLine];
             }

             // zoom in on the route. 
             [self zoomInOnRoute];


         }

     }

     -(void) loadRoute
     {

         AppAppDelegate *obj = (AppAppDelegate*)[[UIApplication sharedApplication] delegate];
         NSArray* pointStrings = [obj.parsedxmldata componentsSeparatedByString:@" "];


         // while we create the route points, we will also be calculating the bounding box of our route
         // so we can easily zoom in on it. 
         MKMapPoint northEastPoint; 
         MKMapPoint southWestPoint; 

         // create a c array of points. 
         MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * pointStrings.count);

         for(int idx = 0; idx < pointStrings.count-1; idx++)
         {
            // break the string down even further to latitude and longitude fields. 
            NSString* currentPointString = [pointStrings objectAtIndex:idx];
            NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];

            CLLocationDegrees latitude  = [[latLonArr objectAtIndex:1] doubleValue];
             CLLocationDegrees longitude = [[latLonArr objectAtIndex:0] doubleValue];

             NSLog(@"%f %f" , latitude , longitude);

    // 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:pointStrings.count];

         _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
     }


     -(void) zoomInOnRoute
     {
        [self.mapview setVisibleMapRect:_routeRect];
     }

     - (void)didReceiveMemoryWarning {
         // Releases the view if it doesn't have a superview.
         [super didReceiveMemoryWarning];

     }


     - (void)dealloc 
     {
        self.mapview = nil;
        self.routeLine = nil;
        self.routeLineView = nil;

     }
     - (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
     {
        MKOverlayView* overlayView = nil;
         NSLog(@"Inside viewforoverlay");

         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 = 3;
             }

             overlayView = self.routeLineView;

         }

         return overlayView;

     }


     @end

1 个答案:

答案 0 :(得分:3)

确保将mapView的委托设置为控制器,可以是XIB,也可以通过编程方式设置,如下所示:

- (void)viewDidLoad
  {
      [super viewDidLoad];

      [_mapview setDelegate:self];

      [self doParse];

    // Do any additional setup after loading the view, typically from a nib.
  }