【发布时间】:2020-08-18 14:44:07
【问题描述】:
我正在制作一个跟踪用户所走路线的应用。对于每个位置更新 horizontalAccuracy >0,我都会删除 MKPointAnnotation。然后我试图从一个点到另一个点绘制 MKPolyline。这从按下开始按钮时开始。 在按下开始按钮之前,MKMapView 会显示当前位置,这是正确的。当我按下开始按钮时,我希望看到点注释和从一个点到另一个点绘制的折线。
但是,在按下开始时,我看到一条从 UTC 坐标(赤道和 UTC 子午线)到我当前位置的折线,这是不可取的。
使用 NSLog 打印时,CLLocationManager 的didUpdateLocation 委托调用不会报告任何此类位置。 这是代码...
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations;
{
CLLocationCoordinate2D coordinates[locations.count];
int index=0;
for (CLLocation *location in locations)
{
#ifdef DEBUG
NSLog(@"received co-ordinates:\nlatitude:%f \nlongitude:%f",location.coordinate.latitude,location.coordinate.longitude);
#endif
[self centerMapWithCoordinates:location.coordinate];
// Add an annotation
if (location.horizontalAccuracy>0)
{
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = location.coordinate;
// if (location.speed>0)
// {
point.title = [NSString stringWithFormat:@"%0.2f Kmph",(location.speed*3600/1000)];
// }
[self centerMapWithCoordinates:location.coordinate];
[mapView addAnnotation:point];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
index++;
}
}
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:index+1];
[mapView addOverlay:polyline];
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay;
{
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
renderer.strokeColor = [[UIColor orangeColor] colorWithAlphaComponent:0.7];
renderer.lineWidth = 3;
return renderer;
}
return nil;
}
我做错了什么?如何摆脱这条不需要的折线?
如果您提供代码,请在 ObjC 中提供。谢谢。
注意:
在导航应用程序后,我注意到所有选定的坐标(通过引脚注释显示)相对于上述相同的 UTC 坐标都有一条折线。看这张图……
【问题讨论】:
-
您想要一条连接不同位置的线路,但您在每次位置更新时都会创建一条新线路。这是为什么呢?
-
该应用程序将跟踪用户步行或跑步锻炼情况。
标签: mapkit mkmapview cllocationmanager cllocation mkpolyline