我已经设置了一个 CLLocation
对象,我正在添加 MKCircle
覆盖。我希望 MKCircle
的 radius
变量与以下两个相关:
- map View 的大小
- 位置的大小
那么,如何从 CLLocation
中检索某种大小值?更好的是,一旦我与 map View 交互/放大,它会自动缩放吗?以下是我的目标概述:
向 map View 添加叠加层。叠加层将位于 CLLocation
CLLocationCoordinate2D
变量中,但我希望该叠加层适合该位置的大小,然后在放大和缩小 map 后调整大小,这我很确定它应该自动执行。我知道我需要定义 MKCircle
的半径变量,但我应该如何设置它呢?那么,你们知道我该怎么做吗?
谢谢!
更新:
CLLocation *org = [[CLLocation alloc] initWithLatitude:coordO.latitude longitude:coordO.longitude];
CLLocation *des = [[CLLocation alloc] initWithLatitude:coordD.latitude longitude:coordD.longitude];
MKCircle *myCircle = [MKCircle circleWithCenterCoordinate:coordO radius:org.horizontalAccuracy];
MKCircle *myCircle2 = [MKCircle circleWithCenterCoordinate:coordD radius:des.horizontalAccuracy];
if (myCircle2 && myCircle) {
NSLog(@"destination and origin both valid");
}
[mapView addOverlays: [NSArray arrayWithObjects:myCircle, myCircle2, nil]];
[myCircle setTitle:@"Placeholder"];
[myCircle2 setTitle:@"Placeholder"];
[localOverlays addObjectsFromArray: [NSArray arrayWithObjects:myCircle, myCircle2, nil]];
这是我用来向 map View 添加叠加层的所有代码,虽然它不起作用,但叠加层(MKCircle
我认为是一个圆圈)不存在/不可见。我查看了 HazardMap Apple 示例代码,看起来他们已经创建了一些其他的设置类。这是必要的吗,我的代码有什么问题。谢谢!
最佳答案
MKCircle
的半径是 CLLocationDistance
,它表示以米为单位的距离测量值。
CLLocation
的水平精度是 CLLocationAccuracy
,表示以米为单位的坐标值的精度。
所以,您需要做的就是:
MKCircle *circle = [MKCircle circleWithCenterCoordinate:location.coordinate
radius:location.horizontalAccuracy];
更新:叠加层不可见,因为您手动创建了位置,因此它们的水平精度为零。您可以使用方法 -[CLLocation initWithCoordinate:altitude:horizontalAccuracy:verticalAccuracy:timestamp:]
创建具有自定义水平精度的 CLLocation
。或者您可以定义圆的最小半径(例如 10 米):
CLLocationDistance radius = MAX(10, location.horizontalAccuracy);
关于objective-c - 关于 MKMapView 的叠加问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10914832/