Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapView visible region padding

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.

enter image description here

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!

like image 542
Alexey Avatar asked Dec 10 '14 08:12

Alexey


People also ask

What is the use of mkmapview?

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.

How do I display a map in as-is?

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.

How do I set the initial region of a map?

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.

How does the map view work?

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.


3 Answers

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.

like image 144
Alexey Avatar answered Oct 20 '22 04:10

Alexey


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)
like image 35
madx Avatar answered Oct 20 '22 04:10

madx


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

like image 37
barksten Avatar answered Oct 20 '22 04:10

barksten