objective-c - 在 Cocoa macOS 应用程序中,如何获取 MKMapView 左下角的度数坐标?

标签 objective-c macos cocoa mkmapview

我用 Objective-C 开发了一个 macOS 应用程序,主窗口中有一个 MKMapView。当我单击按钮时,我想计算可见 map 左下角和右上角的度数坐标。我知道 MKMapView 有一个visibleRect属性,所以我的代码是:

- (IBAction)userDidPressConvertButton:(id)sender {

    MKMapRect mapRect = self.mapView.visibleMapRect;

    CLLocationCoordinate2D coordinates = MKCoordinateForMapPoint(mapRect.origin);

    NSLog(@"Latitude %f Longitude: %f", coordinates.latitude, coordinates.longitude);
    NSLog(@"size width = %f, size height = %f",mapRect.size.width,mapRect.size.height);
}

根据文档,该属性的类型为 MKMapRect。我已经确定它的 origin 成员指向可见矩形的左上角。我的问题是,虽然有 MKCooperativeForMapPoint 函数可以转换为 MKMapPoint 成员原点的坐标,但 size 成员的宽度和高度没有转换为度坐标的函数,并且不以度数表示,但是,根据文档,在 map 点中。如何获取当前可见 map 的左下角和右上角的度数坐标?

非常感谢任何帮助,谢谢

最佳答案

我已经成功找到了从左下角到右上角的可见 map 矩形坐标,在 map 的可见矩形上调用 MKCooperativeRegion ForMapRect 。这是我的代码:

- (IBAction)userDidPressFillCoordinates:(id)sender {

    MKMapRect mapRect = self.mapView.visibleMapRect;

    CLLocationCoordinate2D coordinates = MKCoordinateForMapPoint(mapRect.origin);

    NSLog(@"Latitude %f Longitude: %f", coordinates.latitude, coordinates.longitude);
    NSLog(@"size width = %f, size height = %f",mapRect.size.width,mapRect.size.height);

    NSLog(@"%@",MKStringFromMapRect(mapRect));

    MKCoordinateRegion region =   MKCoordinateRegionForMapRect(mapRect);

    CLLocationCoordinate2D center = region.center;
    MKCoordinateSpan span = region.span;

    CLLocationCoordinate2D topRightCoordinates = coordinates;

    coordinates.latitude -= span.latitudeDelta;

    topRightCoordinates.longitude += span.longitudeDelta;


    self.coordinatesString = [NSString stringWithFormat:@"%f,%f,%f,%f",coordinates.longitude,coordinates.latitude,topRightCoordinates.longitude,topRightCoordinates.latitude];


} 

关于objective-c - 在 Cocoa macOS 应用程序中,如何获取 MKMapView 左下角的度数坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52427368/

相关文章:

ios - 如何在 mac OS 应用程序中使用 canOpenURL?

macos - 尝试执行 cc1 时出错,gcc 只想为 avr 编译?

ios - 为什么 UIKit 委托(delegate) "assign"而不是 "weak"?

iphone - 圈养网络 : CNSetSupportedSSIDs usage

iphone - OSStatus 错误 -50?

linux - 将 MySQL 数据库传输到 Mac 的最佳方式?

swift - 以编程方式将列添加到 NSTableView

objective-c - 第二次使用时 makeKeyAndOrderFront 崩溃...(ARC?)

objective-c - 如何设置 NSTableCellView 的背景颜色?

ios - iOS 中的 Mod 运算符