Dera colleagues,
I'm struggle to implement visible region with native MKMapView. I need to have an exactly the same feature as Google Map SDK Visible Region.
Basically it means to have a sort of padding in the bottom to following code:
let span = MKCoordinateSpan(latitudeDelta: 0.8, longitudeDelta: 0.8)
let reg = MKCoordinateRegion(center: someMyCoordinate, span: span)
mapView.setRegion(reg, animated: true)
It needs to be like this: once I've add some padding in UIView coordinates, it should shift Map center and adjust zoom to make Coordinate Region fully visible, take into account the padding.
Probably my description Is a bit messy, but if you take a look on this video it becomes absolutely clear.
Thank you in advance!
The MKMapView provides a zoomable map interface upon which the application developer can add information-bearing MKAnnotation s and area-based MKOverlay s. In iOS 7 and later, maps support 3D imagery when the MapType property is Standard.
You use this class as-is to display map information and to manipulate the map contents from your application. You can center the map on a given coordinate, specify the size of the area you want to display, and annotate the map with custom information.
When you initialize a map view, you specify the initial region for that map to display by setting the region property of the map. A region is defined by a center point and a horizontal and vertical distance, referred to as the span. The span defines how much of the map should be visible and is also how you set the zoom level.
The map view uses the coordinate data in each annotation object to determine when the corresponding annotation view needs to appear onscreen. When an annotation moves onscreen, the map view asks its delegate to create a corresponding annotation view.
Thanks to approach suggested above, complete solution is following.
import Foundation
import MapKit
extension MKCoordinateRegion{
var mapRect:MKMapRect {
get{
let a = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
self.center.latitude + self.span.latitudeDelta / 2,
self.center.longitude - self.span.longitudeDelta / 2))
let b = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
self.center.latitude - self.span.latitudeDelta / 2,
self.center.longitude + self.span.longitudeDelta / 2))
return MKMapRectMake(min(a.x,b.x), min(a.y,b.y), abs(a.x-b.x), abs(a.y-b.y))
}
}
}
extension MKMapView {
func setVisibleRegion(mapRegion: MKCoordinateRegion, edgePadding insets: UIEdgeInsets, animated animate: Bool) {
self.setVisibleMapRect(mapRegion.mapRect, edgePadding: insets , animated: animate)
}
}
Now you can just use setVisibleRegion func.
If you need a padding from the bottom of 100 in swift you could simply write:
mapView.layoutMargins = UIEdgeInsets(top: 10, right: 10, bottom: 100, left: 10)
Then you can check this centering the map on some annotations like this:
mapView.showAnnotations(mapView.annotations, animated: true)
If you print your visible region you will see that the span will be padded since it needs to fit the view. The center coordinate will still be in the center of the view. I'm not sure what you want to do but I guess it can be archived with
setVisibleMapRect:edgePadding:animated:
You need to have the region converted to a MKMapRect. See Convert MKCoordinateRegion to MKMapRect on how to do that
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With