2013-06-28 80 views
1

我創建了一個不斷讀取當前用戶座標並將它們存儲在SQLite數據庫中的應用程序。
我有一張地圖顯示在整個屏幕上。
現在我想在用戶移動時在地圖上劃一條線。
我已經創建了所有這些。
問題是,我不能讓它成爲'活'。覆蓋未更新。
這是邏輯:
在viewDidLoad中我有在MapKit上繪製路徑的問題

... 
if (nil != self.routeLine) { 
     [self.mapView addOverlay:self.routeLine]; 
    } 

在一個函數處理每一個新的座標,我有:

... 
NSString* coordinate = [NSString stringWithFormat:@"%f,%f", thisLocation.longitude, thisLocation.latitude]; 
      [self.paths addObject:coordinate]; 

MKMapPoint northEastPoint; 
    MKMapPoint southWestPoint; 

    // create a c array of points. 
    MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * self.paths.count); 

    for(int idx = 0; idx < self.paths.count; idx++) 
    { 
     // break the string down even further to latitude and longitude fields. 
     NSString* currentPointString = [self.paths objectAtIndex:idx]; 
     NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]]; 

     CLLocationDegrees latitude = [[latLonArr objectAtIndex:1] doubleValue]; 
     CLLocationDegrees longitude = [[latLonArr objectAtIndex:0] doubleValue]; 

     // create our coordinate and add it to the correct spot in the array 
     CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude); 


     MKMapPoint point = MKMapPointForCoordinate(coordinate); 

     // adjust the bounding box 
     // if it is the first point, just use them, since we have nothing to compare to yet. 
     if (idx == 0) { 
      northEastPoint = point; 
      southWestPoint = point; 
     } 
     else 
     { 
      if (point.x > northEastPoint.x) 
       northEastPoint.x = point.x; 
      if(point.y > northEastPoint.y) 
       northEastPoint.y = point.y; 
      if (point.x < southWestPoint.x) 
       southWestPoint.x = point.x; 
      if (point.y < southWestPoint.y) 
       southWestPoint.y = point.y; 
     } 

     pointArr[idx] = point; 
    } 

    // create the polyline based on the array of points. 
    self.routeLine = [MKPolyline polylineWithPoints:pointArr count:self.paths.count]; 

    _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y); 
    // clear the memory allocated earlier for the points 
    free(pointArr); 

這是viewForOverlay委託功能:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 
    MKOverlayView* overlayView = nil; 

    if(overlay == self.routeLine) 
    { 
     //if we have not yet created an overlay view for this overlay, create it now. 
     if(nil == self.routeLineView) 
     { 
      self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine]; 
      self.routeLineView.fillColor = [UIColor blueColor]; 
      self.routeLineView.strokeColor = [UIColor blueColor]; 
      self.routeLineView.lineWidth = 5; 
     } 

     overlayView = self.routeLineView; 
    } 
    return overlayView; 
} 

回答

2

viewDidLoad,代碼調用addOverlayself.routeLine我假設初始設置爲以前保存的座標。

地圖視圖將routeLine指向的MKPoyline添加到其覆蓋圖的內部列表中,並繪製覆蓋圖。

然後,在「函數處理每個新座標」,self.routeLine改爲指向MKPolyline

原因疊加的觀點並沒有在地圖更新是因爲地圖視圖仍然使用原來MKPolyline傳遞給它的時候addOverlay被調用。


由於MKPolyline本身是不可變的(它不允許一個改變點創建後/座標列表),主要有兩種選擇:

  • 刪除現有的疊加和添加新的座標更新。這是最簡單的選擇。設置self.routeLine到一個新的MKPolyline後,請執行下列操作(例如):

    [self.mapView removeOverlays:mapView.overlays]; 
    self.routeLineView = nil; 
    [self.mapView addOverlay:self.routeLine]; 
    

    這個簡單的方法的主要缺點是覆蓋會有閃爍現象,如果更新頻繁或者如果疊加完成大或複雜。

  • 另一種方法是創建一個可變的自定義覆蓋和覆蓋視圖,並使您能夠更加快速和平滑地動態刷新覆蓋。
    幸運的是,Apple has provided a sample app called Breadcrumb確實如此。