小智 10
要获得半径,请遵循以下步骤:
- (CLLocationDistance)getRadius
{
CLLocationCoordinate2D centerCoor = [self getCenterCoordinate];
// init center location from center coordinate
CLLocation *centerLocation = [[CLLocation alloc] initWithLatitude:centerCoor.latitude longitude:centerCoor.longitude];
CLLocationCoordinate2D topCenterCoor = [self getTopCenterCoordinate];
CLLocation *topCenterLocation = [[CLLocation alloc] initWithLatitude:topCenterCoor.latitude longitude:topCenterCoor.longitude];
CLLocationDistance radius = [centerLocation distanceFromLocation:topCenterLocation];
return radius;
}
Run Code Online (Sandbox Code Playgroud)
它将以米为单位返回半径.
获得中心坐标
- (CLLocationCoordinate2D)getCenterCoordinate
{
return [self.mapView centerCoordinate];
}
Run Code Online (Sandbox Code Playgroud)
获得半径取决于你想获得第二点的位置.让我们走顶级中心
- (CLLocationCoordinate2D)getTopCenterCoordinate
{
// to get coordinate from CGPoint of your map
return [self.mapView convertPoint:CGPointMake(self.mapView.frame.size.width / 2.0f, 0) toCoordinateFromView:self.mapView];
}
Run Code Online (Sandbox Code Playgroud)
Luc*_*nzo 10
使用Swift 3.0,您可以使用扩展来简化您的生活:
extension MKMapView {
func topCenterCoordinate() -> CLLocationCoordinate2D {
return self.convert(CGPoint(x: self.frame.size.width / 2.0, y: 0), toCoordinateFrom: self)
}
func currentRadius() -> Double {
let centerLocation = CLLocation(coordinate: self.centerCoordinate)
let topCenterCoordinate = self.topCenterCoordinate()
let topCenterLocation = CLLocation(coordinate: topCenterCoordinate)
return centerLocation.distance(from: topCenterLocation)
}
}
Run Code Online (Sandbox Code Playgroud)
使用Swift 4.0:
extension MKMapView {
func topCenterCoordinate() -> CLLocationCoordinate2D {
return self.convert(CGPoint(x: self.frame.size.width / 2.0, y: 0), toCoordinateFrom: self)
}
func currentRadius() -> Double {
let centerLocation = CLLocation(latitude: self.centerCoordinate.latitude, longitude: self.centerCoordinate.longitude)
let topCenterCoordinate = self.topCenterCoordinate()
let topCenterLocation = CLLocation(latitude: topCenterCoordinate.latitude, longitude: topCenterCoordinate.longitude)
return centerLocation.distance(from: topCenterLocation)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5781 次 |
| 最近记录: |