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

标签 iphone mapkit

我需要知道从中心 map 到屏幕另一侧的距离(以公里为单位)(如果缩放改变,距离也会改变)。

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

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

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

谢谢

最佳答案

MkMapView 具有名为 centerCooperative(CLLocationCooperative2D) 和 Region (MKCooperativeRegion) 的属性。 Region 是一个结构体:

typedef struct {
    CLLocationDegrees latitudeDelta;
    CLLocationDegrees longitudeDelta;
}MKCoordinateSpan

您应该能够基于 centerCoordinate 创建另一个点,比方说,通过将 latitudeDelta 添加到您的 latitude 属性或 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

并且只需计算委托(delegate)每次触发某个方法的时间(决定您需要哪个:MKMapViewDelegate)

关于iphone - 使用mapkit计算半径(以米为单位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3187932/

相关文章:

objective-c - 如何更改 SFSafariViewController 工具栏颜色

ios - 在退出调用方法之前显示不会更新

iOS - MapKit 相机旋转动画

objective-c - 是否可以将 "Styled Maps"与 MKMapView 一起使用?

ios - 在 iOS 6 中以编程方式打开 map 应用

iphone - 如何使用线程为每一行自动调用一个函数

iphone - 在 iOS 上同时使用手电筒和相机 - 在后台后不起作用

iphone - 使 MKMapView 透明但保持覆盖层不透明

iphone - 当我点击表格 View 中的第一个单元格时,不会触发 didSelectRowAtIndexPath

iphone - 在模拟器上获取用户 MapKit 的_正确_坐标