MapKit上绘制路径的问题

时间:2013-06-28 10:21:28

标签: iphone ios objective-c mkmapview mkoverlay

我创建了一个不断读取当前用户坐标的应用程序,并将它们存储在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;
}

2 个答案:

答案 0 :(得分:2)

viewDidLoad中,代码使用addOverlay调用self.routeLine,我假设它最初设置为先前保存的坐标。

地图视图将MKPoylinerouteLine添加到其内部叠加层列表中并绘制叠加层。

然后,在处理每个新坐标&#34;的&#34;函数中,self.routeLine更改为指向 new 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就是这样做的。

答案 1 :(得分:0)

也许您忘了拨打[self.view setNeedsDisplay]

UIView docs