How to get left-top and right-bottom latitude and longitude of map in MapKit

前端 未结 2 1647
走了就别回头了
走了就别回头了 2020-12-09 20:17

How to get left-top and right-bottom latitude and longitude of map in MapKit? I use this code, but it doesn\'t work properly. How should I fix it?

MKCoordina         


        
相关标签:
2条回答
  • 2020-12-09 20:49

    There is a much easier approach to getting those coordinates... Use the points of your view, and convert:

    CLLocationCoordinate2D topLeft, bottomRight;
    topLeft = [mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:mapView];
    CGPoint pointBottomRight = CGPointMake(mapView.frame.size.width, mapView.frame.size.height);
    bottomRight = [mapView convertPoint:pointBottomRight toCoordinateFromView:mapView];
    
    0 讨论(0)
  • 2020-12-09 20:56

    I created an extension for MKMapView

    Swift 4.2

    extension MKMapView {
        func topLeftCoordinate() -> CLLocationCoordinate2D {
            return convert(.zero, toCoordinateFrom: self)
        }
    
        func bottomRightCoordinate() -> CLLocationCoordinate2D {
            return convert(CGPoint(x: frame.width, y: frame.height), toCoordinateFrom: self)
        }
    }
    

    Swift 2.0

    extension MKMapView {
        func topLeftCoordinate() -> CLLocationCoordinate2D {
            return convertPoint(CGPoint.zero, toCoordinateFromView: self)
        }
    
        func bottomRightCoordinate() -> CLLocationCoordinate2D {
            return convertPoint(CGPoint(x: frame.width, y: frame.height), toCoordinateFromView: self)
        }
    }
    
    0 讨论(0)
提交回复
热议问题