2013-10-17 1 views
0

내 앱은 HomeVC의 MKOverlayRenderer로 MKMapView의 MKPolyline 라우팅 경로로 GPS 이동을 추적하고 데이터를 저장 한 다음 나중에 몇 개의 VC가 저장된 경로로 DisplayVC에 표시합니다. 데이터가 두 번째 VC의 원래 데이터와 동일하다는 것을 확인할 수 있으며지도가 표시 될 때 적절한 routeBounds가 사용되지만 두 번째 VC에서는 OverlayRenderer가 호출되지 않습니다. 왜 안돼? 나는 대의원 문제를 생각하고있다. 그러나 나는 틀린 것을 발견 할 수 없다.동일한 MKOverlayView 대리인이 하나의 UIView에서 호출되었지만 다른 MView에서는 호출되지 않았습니다. 누락 된 것이 무엇입니까?

두 homeVC.h

@interface homeVC : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate> { 

및 displayVC.h이 이름을 제외하고는 동일합니다까지 동일하게 설정되어

@interface displayVC : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate> { 
    CLLocationManager *locationManager; 
    // the data representing the route points 
    MKPolyline*  _routePath; 
    // the view we create for the line on the map 
    MKPolylineView* _routePathVw; 
    // the rect that bounds the loaded points 
    MKMapRect  _routeBounds; 
} 
@property (nonatomic, weak) IBOutlet MKMapView *mapView; 
@end 

그리고 homeVC.m 및 displayVC.m 모두 :

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Add the Map 
    [_mapView setDelegate:self]; 
    _mapView.mapType = MKMapTypeStandard; 
} 

많은 유용한 코드가 있습니다. 그런 다음

-(void) buildRoute { 
    CLLocationCoordinate2D thisCoord; 
    int i = [arrayLa count] - 1; // keep growing the array size 
    MKMapPoint *tmpArr = realloc(pointArr, sizeof(CLLocationCoordinate2D)*(arrayLa.count)); 
    pointArr = tmpArr; 
    thisCoord.latitude = [[arrayLa objectAtIndex:i] floatValue]; 
    thisCoord.longitude = [[arrayLo objectAtIndex:i] floatValue]; 
    MKMapPoint point = MKMapPointForCoordinate(thisCoord); 
    pointArr[i] = point; 
    // Reset Map View Boundaries 
    if(point.x > ne_Pt.x - 500) ne_Pt.x = point.x + 1000; 
    if(point.y > ne_Pt.y - 500) ne_Pt.y = point.y + 1000; 
    if(point.x < sw_Pt.x + 500) sw_Pt.x = point.x - 1000; 
    if(point.y < sw_Pt.y + 500) sw_Pt.y = point.y - 1000; 
    // create the polyline based on the C-array of map Points 
    _routePath = [MKPolyline polylineWithPoints:pointArr count:arrayLa.count]; 
    _routeBounds = MKMapRectMake(sw_Pt.x, sw_Pt.y, ne_Pt.x-sw_Pt.x, ne_Pt.y-sw_Pt.y); 
    // add the routePath overlay to the map, if it isn't empty 
    if (recState == REC && _routePath != nil) { 
     // zoom in on the route with the fresh bounding box, routeBounds 
     [self zoomInOnRoute]; 
     [_mapView addOverlay:_routePath]; 
    } 
} 

-(void) zoomInOnRoute { 
    [_mapView setVisibleMapRect:_routeBounds]; 
} 

#pragma mark MKMapViewDelegate 
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay { 
    if ([overlay isKindOfClass:[MKPolyline class]]) { 
     MKPolyline *route = overlay; 
     MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route]; 
     routeRenderer.lineWidth = 3; 
     routeRenderer.strokeColor = [UIColor redColor]; 
     return routeRenderer; 
    } 
    else return nil; 
} 

누구든지 내 문제를 해결할 수 있습니까? 감사합니다.

답변

0

위임자 문제처럼 보입니다. 'if'가 그것을 건너 뛰는 경우 addOverLay 호출에 중단 점을 넣으려고 했습니까?

나는 비슷한 것을 해내 고 MKOverlay와 MKOverlayRender (사과 빵 부스러기 견본 응용 프로그램을 기반으로하지만 업데이트 됨)를 사용하여 모두 잘 작동합니다. 앱은 사용자가 CoreData에 저장할 수있는 경로를 표시합니다. 저장된 경로 테이블에서 선택할 수 있으며 경로는 MKOverlayRenderer를 사용하여 렌더링됩니다.

대리자

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    [self.mapView setDelegate:self]; 
} 

MKOverlay를 만들고지도보기

- (void)createRoute2 
{ 
    // Some CoreData stuff. Iterate through map points and create an overlay. 
    // Order by date ascending so we draw in sequential order 
    NSSortDescriptor *timeStampDescriptor = [[NSSortDescriptor alloc] initWithKey:@"pointDate" ascending:YES]; 
    NSArray *sortDescriptors = @[timeStampDescriptor]; 
    NSArray *routePts = [[self.selectedRoute mapDetail] sortedArrayUsingDescriptors:sortDescriptors]; 
    // A long is a bit excessive just use an int is fine (BUT might be a huge number of route points) 
    long nbrPts = routePts.count; 

    if (nbrPts < 2){ 
     return; 
    } 

    CLLocationCoordinate2D mapPointLoc; 
    MKMapRect updateRect; // The map area 

    // Init the route 
    // FtfmapDetail is a managed object holding lat long (and other stuff) 
    FtfMapDetail *currentPt = (FtfMapDetail *)[routePts objectAtIndex:0]; 
    mapPointLoc = CLLocationCoordinate2DMake([currentPt.latitude floatValue], [currentPt.longitude floatValue]); 
    // self.route is a subclassed MapOverlay 
    if (!self.route) 
    { 
     // BGSMapOverlay is a subclassed MapOverlay 
     self.route = [[BGSMapOverlay alloc] initWithCenterCoordinate:mapPointLoc]; 
     [self.mapView addOverlay:self.route]; 
    } 

    // Add subsequent points. Kick off the for loop at int position 1 not 0 
    for (int i=1; i <nbrPts; i++){ 

     currentPt = (FtfMapDetail *)[routePts objectAtIndex:i]; 
     mapPointLoc = CLLocationCoordinate2DMake([currentPt.latitude floatValue], [currentPt.longitude floatValue]); 
     // AddCoordinate is a method in MKOverlay subclass that returns a bounding MKMaprect for all points in MkOverlay 
     updateRect = [self.route addCoordinate:mapPointLoc]; 
    } 

    MKCoordinateRegion region = MKCoordinateRegionForMapRect(self.route.boundingMapRectCompleteRoute); 

    [self.mapView setRegion:region animated:YES]; 

} 

를 추가하고 대리자 메서드

#pragma mark - MKMapView delegate 

// self.routeViewRenderer is a sub-classed MKOverlayRendered (based on the Breadcrumbs app from apple CrumbPathView subclassed MKOverlayView) 
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{ 
    if (!self.routeViewRenderer) 
    { 
     _routeViewRenderer = [[BGSMKOverlayRender alloc] initWithOverlay:overlay]; 
    } 
    return self.routeViewRenderer; 

} 

를 추가해야합니다 또는 당신이 것을 수를 설정 MkPolyLine에 좌표를 보내지 않습니다. 다음 스 니펫에서 "NSArray * routeCoordinates = [[NSArray alloc] init];" MKPolyLine에 Nil 배열을 보내면 코드는 실행되지만 델리게이트는 호출되지 않습니다. routeCoordinates에 포인트가 포함되어 있으면 델리게이트가 호출되고 경로가 표시됩니다.

-(void)buildRouteOverlays 
{ 
    for (int i=0; i< _routeHeaders.count;i++) 
    { 
     _selectedRoute = (FtfMaps*) [_routeHeaders objectAtIndex:i]; 
     NSLog(@"DEBUG route date : %@", _selectedRoute.dateMap); 
     NSArray *routeCoordinates = [self arrayRoutePointCoordinates]; 
     // if a nil array is produce then MapOverlayRenerder is not called - nothing to render 
     // Test this by uncommenting: 
     // NSArray *routeCoordinates = [[NSArray alloc]init]; 

     NSLog(@"DEBUG number of point in Route : %lu",(unsigned long)routeCoordinates.count); 
     // Just a quick test only process the first route 
     if (i==0){ 
      MKPolyline *routePolyLine = [self polyLineFromArray:routeCoordinates]; 
      [self.mapView addOverlay:routePolyLine]; 
     } 
    } 
} 

-(MKPolyline*)polyLineFromArray:(NSArray*)routePoints 
{ 
    NSInteger pointsCount = routePoints.count; 

    CLLocationCoordinate2D pointsToUse[pointsCount]; 

    for(int i = 0; i < pointsCount; i++) { 
     FtfMapDetail *mapPt = (FtfMapDetail *) [routePoints objectAtIndex:i]; 
     pointsToUse[i] = CLLocationCoordinate2DMake([mapPt.latitude doubleValue], [mapPt.longitude doubleValue]); 
    } 
    MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:pointsToUse count:pointsCount]; 
    return myPolyline; 

} 


#pragma mark MKMapViewDelegate 
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay { 
    if ([overlay isKindOfClass:[MKPolyline class]]) { 
     MKPolyline *route = overlay; 
     MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route]; 
     routeRenderer.lineWidth = 3; 
     routeRenderer.strokeColor = [UIColor redColor]; 
     return routeRenderer; 
    } 
    else return nil; 
} 
+0

감사합니다. 실행되지 않은 if() 전에 NSLog가있었습니다. –

+0

답변에 조금 더 추가되었습니다. 아마도 polyLine에 포인트를 보내지 않았습니까? 희망이 도움이됩니다. – greentor

+0

고마워, 다시,하지만 모든 포인트가 올바르게 경로에 있다고 확신합니다. –

관련 문제