2013-05-10 43 views
2

我試圖使用疊加(MKOverlay)在自由手上追蹤MKMapView無權在ios中繪製多段線疊加

當我們移動手指我去年與新座標的座標折線延長每一次,都正在擴展折線覆蓋整個覆蓋在設備閃爍(只是偶爾)時,除了做工精細,這樣我就可以,T跟蹤問題。

我試過的代碼在下面給出。

- (void)viewDidLoad 

{ 

    j=0;  
    coords1 = malloc(2* sizeof(CLLocationCoordinate2D)); 

    coordinatearray=[[NSMutableArray alloc]init]; 

    UIPanGestureRecognizer *GestureRecogonized = [[UIPanGestureRecognizer alloc]  initWithTarget:self action:@selector(gestureDetacted:)]; 

    [self.myMapView addGestureRecognizer:GestureRecogonized]; 

} 

- (void)gestureDetacted:(UIPanGestureRecognizer *)recognizer 
{ 

    if(UIGestureRecognizerStateBegan==recognizer.state) 
    { 

     CGPoint point = [recognizer locationInView:self.myMapView]; 
     CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view]; 

     CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:tapPoint.latitude longitude:tapPoint.longitude]; 

     [coordinatearray addObject:curLocation]; 
    } 

    coords1[0]=[[coordinatearray objectAtIndex:j] coordinate]; 

    if(UIGestureRecognizerStateChanged==recognizer.state) 
    { 
     j++; 

     CGPoint point = [recognizer locationInView:self.myMapView]; 
     CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view]; 

     CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:tapPoint.latitude longitude:tapPoint.longitude]; 

     [coordinatearray addObject:curLocation]; 
     coords1[1]=CLLocationCoordinate2DMake(tapPoint.latitude,tapPoint.longitude); 

     polyLine = [MKPolyline polylineWithCoordinates:coords1 count:2]; 

     [self.myMapView addOverlay:polyLine]; 
    } 
} 

在覆蓋委託

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay { 

    if([overlay isKindOfClass:[MKPolyline class]]){ 

     MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay]; 

     polylineView.strokeColor = [UIColor orangeColor]; 
     polylineView.lineWidth = 20; 

     polylineView.fillColor=[[UIColor orangeColor] colorWithAlphaComponent:.1]; 

     return polylineView; 
    } 
} 

有誰能夠知道這是爲什麼閃爍或閃爍效果快到了,如何刪除它。

在此先感謝。

回答

1

與其添加數百個非常小的視圖(這實際上是計算密集型的),我寧願刪除多段線疊加層,並在泛識別器的每次更改中添加一個新的地圖上的所有點更平滑的效果,你可以先添加新的,然後刪除舊的)。使用coordinateArray創建包含所有點的MKPolyline覆蓋圖,而不是最後2個點。 你可以這樣做:

[coordinatearray addObject:curLocation];; 
CLLocationCoordinate2D* coordArray = malloc(sizeof(CLLocationCoordinate2D)*[coordinatearray count]); 
memcpy(coordArray, self.coordinates, sizeof(CLLocationCoordinate2D)*([coordinatearray count]-1));  
coordArray[[coordinatearray count]-1] = curLocation; 
free(self.coordinates); 
self.coordinates = coordArray; 
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordArray count:[coordinatearray count]]; 
MKPolyline *old = [[self.mapView overlays] lastObject]; 
[self.mapView addOverlay:polyline]; 
[self.mapView removeOverlay:old]; 

哪裏self.coordinate的類型是CLLocationCoordinate2D *的屬性,這樣你可以memcpy的現有的陣列到一個新的(memcpy的是真的efficent),只需要添加最後一點指向數組,而不必遍歷NSArray *coordinatearray的每個點。

您還必須更改您的if(UIGestureRecognizerStateBegan==recognizer.state)部分,才能在self.coordinates中添加第一個分接點。 就類似:

self.coordinates = malloc(sizeof(CLLocationCoordinate2D)); 
self.coordinates[0] = curLocation; 

編輯:我認爲這個問題是地圖疊加得出自己的縮放級別的不同的離散值。在特定的縮放級別下,疊加層首先會以較大的縮放級別繪製自己,然後在較小的縮放級別(實際上繪製先前繪製的疊加層)。當您嘗試在此縮放級別繪製用戶平移手勢來顯示動畫時,這就是覆蓋圖保持閃爍的原因。一種可能的解決方案是使用放置在地圖視圖頂部的透明視圖,其中在用戶不斷移動手指的同時執行繪圖。只有平移手勢結束後,纔會創建一個地圖疊加圖,您可以「貼」到地圖上並清理繪圖視圖。重繪視圖時還需要小心,因爲每次只需指定要重繪的矩形,然後僅在該矩形中重繪,因爲每次重繪整個視圖都會在該視圖中導致閃爍。這肯定涉及更多的編碼,但它應該起作用。檢查這question如何增量繪製視圖。

+0

感謝您的回覆,唯一的問題是,我們通過自由手繪製折線時,所有先前添加的折線也會閃爍(閃爍),因此繪圖不會像連續一樣感覺。大部分時間工作正常,問題僅在保持地圖縮放水平在特定的水平,所以我不能追查真正的問題。我測試了設備中的代碼而不是模擬器。 – 2013-05-10 10:48:41

+0

你說得對,我只注意到在特定的縮放級別,每次更新都會閃爍。但我相信我的方法應該會帶來更好的性能 – micantox 2013-05-10 10:55:13

+0

謝謝,我永遠不會刪除我的代碼中添加的任何覆蓋圖 – 2013-05-10 11:05:37