0

我的应用程序将 GPS 移动作为 MKMapView 上的 MKPolyline 路径路径作为 HomeVC 中的 MKOverlayRenderer 进行跟踪,保存数据并稍后在 DisplayVC 上显示为更深几个 VC 的保存路径路径。我可以确认数据与第二个 VC 上的原始数据相同,并且在显示地图时使用了正确的 routeBounds,但从未在第二个 VC 上调用 OverlayRenderer。为什么不?我在考虑委托问题,但我找不到任何问题。

两者都是 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;
}

任何人都可以帮助解决我的问题吗?谢谢!

4

1 回答 1

0

它看起来确实像一个委托问题。您是否尝试在 addOverLay 调用上设置断点以防“if”跳过它?

我使用 MKOverlay 和 MKOverlayRender 做了类似的事情并且一切正常(基于苹果面包屑示例应用程序,但已更新)。该应用程序显示用户可以保存到 CoreData 的路线。他们可以从保存的路线表中进行选择,并使用 MKOverlayRenderer 渲染路线。

设置委托

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

创建一个 MKOverlay 并添加到 mapView

- (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];” 将 Nil 数组发送到 MKPolyLine 然后代码将运行,但不会调用委托。如果 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;
}
于 2013-10-20T14:19:51.287 回答