1

我一直在构建一个应用程序来绘制当前用户沿地图移动的线条。现在,在 iOS5 中,当我将叠加层添加到地图视图时,它只是直接添加它,在 iOS6 中,它添加了带有“淡入”动画的叠加层。我不想要这个烦人的动画。我还有一个功能可以在保存的坐标上播放轨迹(在用户完成跑步或步行之后),当我播放这个时,线条闪烁得非常快,只是因为这个动画。这真的很烦人,我真的很感激任何指导或解决方案来摆脱这个动画。

添加叠加层的代码:

MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D)*2);
pointsArray[0]= MKMapPointForCoordinate(oldLocation.coordinate); 
pointsArray[1]= MKMapPointForCoordinate(newLocation.coordinate);
self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
free(pointsArray);    
[self.mapView addOverlay:self.routeLine];

显示叠加的代码:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
    MKOverlayView *overlayView = nil;
    MKPolylineView  *routeLineView = [[MKPolylineView alloc] initWithPolyline:(MKPolyline *)overlay];
    routeLineView.fillColor = [UIColor blueColor];
    routeLineView.strokeColor = [UIColor blueColor];
    routeLineView.backgroundColor = [UIColor clearColor];
    routeLineView.lineWidth = 10;
    routeLineView.lineCap = kCGLineCapRound;
    overlayView = routeLineView;
    return overlayView;
}

谢谢!

编辑:

通过路径的代码:

playTimer = [NSTimer scheduledTimerWithTimeInterval:interval
                                                 target:self
                                               selector:@selector(playPathTimer)
                                               userInfo:nil
                                                repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:playTimer forMode:NSRunLoopCommonModes];

- (void)playPathTimer{
    MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D) * 2);
    double latitude1 = ((WorldPoint *)[coordsArray objectAtIndex:playCounter]).latitude.doubleValue;
    double longitude1 = ((WorldPoint *)[coordsArray objectAtIndex:playCounter]).longitude.doubleValue;
    double latitude2 = ((WorldPoint *)[coordsArray objectAtIndex:playCounter+nextCord]).latitude.doubleValue;
    double longitude2 = ((WorldPoint *)[coordsArray objectAtIndex:playCounter+nextCord]).longitude.doubleValue;
    CLLocation *location1 = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude1, longitude1)
                                                          altitude:0
                                                horizontalAccuracy:0
                                                  verticalAccuracy:0
                                                         timestamp:[NSDate date]];
    CLLocation *location2 = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude2, longitude2)
                                                          altitude:0
                                                horizontalAccuracy:0
                                                  verticalAccuracy:0
                                                         timestamp:[NSDate date]];
    pointsArray[0] = MKMapPointForCoordinate(location1.coordinate);
    pointsArray[1] = MKMapPointForCoordinate(location2.coordinate);
    self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
    free(pointsArray);

    [self.mapView addOverlay:self.routeLine];
    [playRouteLines addObject:self.routeLine];


//    self.mapView.centerCoordinate = CLLocationCoordinate2DMake(latitude1, longitude1);

    playCounter = playCounter + nextCord;
    if(playCounter + nextCord >= coordsArray.count){
//        [displayLink setPaused:YES];
//        [displayLink invalidate];
       [playTimer invalidate];
        playTimer = nil;
        playCounter = 0;
        [self showStartAndEndFlags];
        if(ee.trail.checkpoint.count > 0){
            [self showCheckpoints];
        }
        self.playButton.enabled = YES;
        MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:[self getRegionWithCorrectZooming]];
        [self.mapView setRegion:adjustedRegion animated:YES];
    }
}
4

1 回答 1

0

您可以使用您自己的 MKOverlayView 子类(MKPolylineView 的超类),而不是使用 MKPolylineView,然后覆盖您自己绘制线条的 drawMapRect:zoomScale:inContext:。

我想有了这个你不会有任何淡入淡出的动画(我正在使用它并且从未注意到任何动画)。

为您的 drawMapRect:zoomScale:inContext: 实现尝试以下代码。您需要创建属性 lineColor(CGColor) 和 lineWidth (CGFloat)。

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    MKPolyline *polyline = self.overlay;
    CGContextSetStrokeColorWithColor(context, self.lineColor);
    CGContextSetLineWidth(context, self.lineWidth/zoomScale);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPoint *points = malloc(sizeof(CGPoint)*polyline.pointCount);
    for (int i=0; i<polyline.pointCount; i++) {
        points[i] = [self pointForMapPoint:polyline.points[i]];
    }
    CGPathAddLines(path, NULL, points, polyline.pointCount);

    CGContextAddPath(context, path);

    CGContextDrawPath(context, kCGPathStroke);
    CGPathRelease(path);
    free(points);
}
于 2013-01-24T14:14:23.593 回答