如何解决跟踪用户位置 MKPolyline从UTC坐标0,0绘制到当前位置
我正在制作一个跟踪用户走的路线的应用。我为 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坐标都有一条折线。看到这张图片...
解决方法
即使在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];
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。