1

iOSの地図上に2つのパスの間のルートを描きたい. 問題は、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
4

1 に答える 1

3

次のように、XIBまたはプログラムでmapViewのデリゲートがコントローラーに設定されていることを確認してください。

- (void)viewDidLoad
  {
      [super viewDidLoad];

      [_mapview setDelegate:self];

      [self doParse];

    // Do any additional setup after loading the view, typically from a nib.
  }
于 2012-06-01T20:06:06.230 に答える