Robert
Robert

Reputation: 148

iOS MapKit get actual visible area of MKMapView when mapView is 3D

I've tried the following to get the actual visible area of my MKMapView after a region change. None produce the desired result after the user rotates the map.

  1. use mapView.bounds and mapView.convertPoint to get NE and SW CLLocationCoordinate2D.
  2. use mapView.visibleRect to create NE and SW MKMapPoints and convert those points to NE and SW CLLocationCoordinate2D.
  3. use mapView.centerCoodinate and mapView.region.span latitude and longitude delta to calculate NE and SW latitude and longitude, which are then used for new NE and SW CLLocationCoordinate2D.

#1 and #2 come from this post, and all 3 work well enough until the user rotates the map, which brings the mapView.camera into play by changing its heading. Once this happens, the mapView.visibleRect does not match the actual visible area. I'm sure changing altitude and pitch will have similar issues. I understand why properties on MKMapView don't make sense once it goes 3D, but I don't know how to account for the mapView.camera. There is mention of this in a comment on one of the proposed answers in this post, but no solution provided.

My question is, how can I get the area that's actually visible to the user, through the mapView.camera, accounting for heading, altitude and pitch?

Upvotes: 4

Views: 1024

Answers (1)

tsuyoski
tsuyoski

Reputation: 644

I was looking for an answer to the similar situation, and found this. For my project I resolved like the following. Hope this helps.

    let northWestCoordinate = self.mapView.convert(CGPoint(x: 0, y: 0), toCoordinateFrom: self.mapView)
    let southEastCoordinate = self.mapView.convert(CGPoint(x: self.mapView.frame.size.width, y: self.mapView.frame.size.height), toCoordinateFrom: self.mapView)

Upvotes: 2

Related Questions