2013-01-24 95 views
1

我一直在構建一個繪製當前用戶沿着地圖移動的線條的應用程序。現在,在iOS5中,當我將疊加層添加到mapview時,它只是直接添加疊加層,在iOS6中,疊加層添加了「淡入」動畫。我不想要這個惱人的動畫。我還有一個功能,可以在保存的座標(用戶完成跑步或步行之後)上播放曲目,當我播放這些曲目時,線條閃爍非常快,僅僅是因爲這個動畫。這真的很煩人,我真的很感謝任何指導或解決方案來擺脫這種動畫。iOS 6 - 地圖視圖,覆蓋淡入

代碼添加疊加:

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]; 
    } 
} 
+0

這是否只發生在模擬器或設備?我有類似的東西,我沒有看到主要的閃爍。 –

+0

它只發生在設備上,而不是模擬器 – Carnal

+0

我會研究它,但也會顯示您用於通過路徑的代碼。這可能與此有關。 –

回答

0

而不是使用MKPolylineView您可以使用自己的MKOverlayView的子類(MKPolylineView的超類),然後覆蓋drawMapRect:zoomScale:inContext的:你自己畫線的地方。

我想這個,你不會有任何淡入淡出動畫(我使用它,從來沒有注意到任何動畫)。

爲drawMapRect嘗試以下代碼:zoomScale:inContext:implementation。 您需要創建屬性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); 
} 
+0

然後我需要子類MKMapView? – Carnal

+0

不需要。您需要繼承MKOverlayView,而不是使用MKPolylineView來使用新的子類。我將重新修改以使答案更清楚。 –

+0

你能否給我一個關於你重寫的方法的例子嗎? – Carnal