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

ger*_*dpg 5 iphone mapkit

我需要知道从中心地图到屏幕另一侧的距离(以千米为单位)(如果变焦改变,距离将改变).

我需要在这个函数中实现这个功能

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢

小智 18

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

typedef struct {
    CLLocationDegrees latitudeDelta;
    CLLocationDegrees longitudeDelta;
}MKCoordinateSpan
Run Code Online (Sandbox Code Playgroud)

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

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
Run Code Online (Sandbox Code Playgroud)

像这样的东西

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
Run Code Online (Sandbox Code Playgroud)

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

  • `centerCoordinate.latitude + region.span.latitudeDelta`必须是`centerCoordinate.latitude + region.span.latitudeDelta/2`才能获得正确的半径. (3认同)