使用mapkit计算半径(以米为单位)

时间:2010-07-06 15:55:54

标签: iphone mapkit

我需要知道从中心地图到屏幕另一侧的距离(以千米为单位)(如果变焦距离发生变化,则会发生变化)。

我需要在此功能中实现此功能

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
}

我有什么想法可以做到这一点?

由于

2 个答案:

答案 0 :(得分:18)

MkMapView具有名为centerCoordinate(CLLocationCoordinate2D)和region(MKCoordinateRegion)的属性。 Region是一个结构:

typedef struct {
    CLLocationDegrees latitudeDelta;
    CLLocationDegrees longitudeDelta;
}MKCoordinateSpan

您应该能够创建另一个基于centerCoordinate的点,比如说,通过将latitudeDelta添加到纬度属性或centerCoordinate,并使用CLLocation方法计算距离:

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

像这样的东西

MkMapView * mapView; // init somewhere
MKCoordinateRegion region = mapView.region;
CLLocationCoordinate2D centerCoordinate = mapView.centerCoordinate;
CLLocation * newLocation = [[[CLLocation alloc] initWithLatitude:centerCoordinate.latitude+region.span.latitudeDelta longitude:centerCoordinate.longitude] autorelease];
CLLocation * centerLocation = [[[CLLocation alloc] initWithLatitude:centerCoordinate.latitude:longitude:centerCoordinate.longitude] autorelease];
CLLocationDistance distance = [centerLocation distanceFromLocation:newLocation]; // in meters

只需计算代表每次触发某个方法(决定你需要哪个:MKMapViewDelegate)

答案 1 :(得分:0)

因为矩形不是圆形,所以无法获得半径。

但是你仍然可以离一个地区的中心距离最远。 重复使用与上述相同的想法,但嵌入了快速扩展。

extension MKCoordinateRegion {
    func distanceMax() -> CLLocationDistance {
        let furthest = CLLocation(latitude: center.latitude + (span.latitudeDelta/2),
                             longitude: center.longitude + (span.longitudeDelta/2))
        let centerLoc = CLLocation(latitude: center.latitude, longitude: center.longitude)
        return centerLoc.distanceFromLocation(furthest)
    }
}

用法

let radius = mapView.region.distanceMax()
相关问题