mapkit - 跟踪用户位置。MKPolyline 从 UTC 坐标 (0, 0) 绘制到当前位置
问题描述
我正在制作一个跟踪用户所走路线的应用程序。我正在为每个位置更新删除MKPointAnnotation ,horizo ntalAccuracy > 0。然后我试图从一个点到另一个点绘制一条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 坐标都有一条折线。看到这张图...
解决方案
即使在 Apple 开发者论坛上,我也无法得到答案。我与 Apple 一起打开了一个技术支持实例,结果发现我正在增加索引,该索引会将 C 数组的大小设置为比所需的多一倍,这导致从坐标 (0.0, 0.0) 绘制多段线。
我把代码改成了这个...
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations;
{
#ifdef DEBUG
NSLog(@"locations array %@",locations);
#endif
for (int i=0; i<locations.count;i++)
{
if (i+1 <locations.count) {
if (locations[i].horizontalAccuracy>locations[i+1].horizontalAccuracy) {
currentLocation = locations[i+1];
}
}
else
{
currentLocation = locations[i];
}
}
#ifdef DEBUG
NSLog(@"current location: %@",currentLocation);
#endif
[locationArray addObject:currentLocation];
[self centerMapWithCoordinates:currentLocation.coordinate];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = currentLocation.coordinate;
[mapView addAnnotation:point];
CLLocationCoordinate2D coordinates[2];
if (previousLocation!=nil)
{
coordinates[0]= previousLocation.coordinate;
coordinates[1]=currentLocation.coordinate;
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:2];
[mapView addOverlay:polyline];
}
else
{
previousLocation=[currentLocation copy];
}
}
推荐阅读
- android - 处理程序第一次运行函数错误kotlin
- javascript - 转换 API 数据并每 60 秒获取一次 [react]
- javascript - 如何在 CSS 函数中读取样式的值
- javascript - ffmpeg 命令行代码转换为 fluent-ffmpeg
- python - 如何安装 CPLEX for Python 以便可以通过 Jupyter 访问它?
- asp.net - chrome 强制 css 和 js 文件出现 https 错误(错误 - ERR_CERT_COMMON_NAME_INVALID)
- asp.net-core - 如何在 ASP.NET Core 和 Postman 中使用 PATCH?
- windows-subsystem-for-linux - 在 Windows 10 上运行 Ubuntu 应用程序的问题
- python - VSCode:使用 MS Python 扩展无法执行代码
- node.js - 使节点服务器不缓存文件