簡體   English   中英

MapKit上繪制路徑的問題

[英]Issue with drawing path over MapKit

我創建了一個應用程序,該應用程序不斷讀取當前用戶坐標並將其存儲在SQLite數據庫中。
我有一個顯示在整個屏幕上的地圖。
現在,我想在用戶移動時在地圖上畫一條線。
我已經創建了所有這些。
問題是我無法使其成為“生活”。 疊加層未更新。
這是邏輯:
在ViewDidLoad中,我有

...
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;
}

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提供了一個名為Breadcrumb的示例應用程序 ,該應用程序正是這樣做的。

也許您只是忘了打電話給[self.view setNeedsDisplay]

UIView文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM