Мне нужны координаты upperRight и bottomLeft видимого mapView.
В mapViewDidChangeVisibleRegion я получаю значения.
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var currentLocation: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
self.customizeMapView()
currentLocation = CLLocation(latitude: ( 37.334922), longitude: (-122.009033))
self.centerMapOnLocation()
}
func customizeMapView(){
self.mapView.showsBuildings = true
self.mapView.showsCompass = true
self.mapView.showsPointsOfInterest = true
self.mapView.showsUserLocation = false
self.mapView.userTrackingMode = .none
self.mapView.showsZoomControls = true
self.mapView.mapType = .satelliteFlyover
}
func centerMapOnLocation(){
let centerRegionCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
let spanRegion:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.0025, longitudeDelta: 0.0025)
let mapRegion: MKCoordinateRegion = MKCoordinateRegion(center: centerRegionCoordinate, span: spanRegion)
mapView.setRegion(mapRegion, animated: true)
}
func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
print("mapViewDidChangeVisibleRegion")
/*
0,0 +x
+y
*/
let upperRight = mapView.convert(CGPoint(x: self.view.frame.size.width, y: 0.0), toCoordinateFrom: self.view)
let bottomLeft = mapView.convert(CGPoint(x: 0.0, y: self.view.frame.size.height), toCoordinateFrom: self.view)
print("upperRight: \(upperRight) ")
print("bottomLeft: \(bottomLeft) ")
}
Я ожидаю, что координаты будут правильными, но я получаю -180.0.
Это значения, которые я получаю при первом запуске приложения:
upperRight: CLLocationCoordinate2D(latitude: -180.0, longitude: -180.0)
bottomLeft: CLLocationCoordinate2D(latitude: -180.0, longitude: -180.0)
Перемещение карты по горизонтали с помощью жеста:
upperRight: CLLocationCoordinate2D(latitude: -180.0, longitude: -180.0)
bottomLeft: CLLocationCoordinate2D(latitude: 37.33245941071868, longitude: -122.01073312548439)
Перемещение карты по вертикали с помощью жеста:
upperRight: CLLocationCoordinate2D(latitude: 37.33687329393082, longitude: -122.00780068382419)
bottomLeft: CLLocationCoordinate2D(latitude: -180.0, longitude: -180.0)
Перемещение карты по диагонали с помощью жеста:
upperRight: CLLocationCoordinate2D(latitude: 37.336306795130724, longitude: -122.00839348216184)
bottomLeft: CLLocationCoordinate2D(latitude: 37.33126864523067, longitude: -122.01130820828396)
Как правильно выполнить mapView.convert?