gpt4 book ai didi

iphone - MapKit 上的绘图路径问题

转载 作者:行者123 更新时间:2023-11-29 10:57:57 25 4
gpt4 key购买 nike

我创建了一个不断读取当前用户坐标并将其存储在 SQLite 数据库中的应用程序。
我有一张显示在整个屏幕上的 map 。
现在我想在用户移动时在 map 上画一条线。
我已经创造了这一切。
问题是我不能让它成为“现场”。叠加层未更新。
这是逻辑:
在 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 委托(delegate)函数:

- (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 中,代码使用 self.routeLine 调用 addOverlay,我假设它最初设置为先前保存的坐标。

map View 将 routeLine 指向的 MKPoyline 添加到其内部叠加层列表中并绘制叠加层。

然后,在“处理每个新坐标的函数”中,self.routeLine 被更改为指向一个 MKPolyline

覆盖 View 未被 map 更新的原因是因为 map View 仍在使用原始MKPolyline,在addOverlay 被调用了。


由于 MKPolyline 本身是不可变的(它不允许在创建后更改点/坐标列表),因此有两个主要选项:

  • 移除现有叠加层并添加一个具有更新坐标的新叠加层。这是最简单的选择。将 self.routeLine 设置为新的 MKPolyline 后,执行以下操作(例如):

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

    这种简单方法的主要缺点是,如果更新频繁进行,或者如果叠加层很大或很复杂,则叠加层似乎会闪烁。

  • 另一种方法是创建可变的自定义覆盖层和覆盖层 View ,使您能够更快、更顺畅地动态刷新覆盖层。
    还好,Apple has provided a sample app called Breadcrumb正是这样做的。

关于iphone - MapKit 上的绘图路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17362490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com