2
votes

I'm having some trouble displaying a fixed map of Ireland using Mapbox in ios on different screen sizes. MGLMapView.setVisibleCoordinates seems to be what I need but it's not behaving as I'd expect. I'm expecting to pass in a bottom left coordinate and a top right coordinate and have that define a rectangle to display regardless of screen size.

mapView.setVisibleCoordinates([CLLocationCoordinate2D(latitude: 50.347676, longitude: -11.444362), CLLocationCoordinate2D(latitude: 56.375322, longitude: -4.128659)], count: 2, edgePadding: UIEdgeInsets.zero, animated: false)

I've also tried

mapView.setVisibleCoordinateBounds(MGLCoordinateBounds(sw: CLLocationCoordinate2D(latitude: 50.347676, longitude: -11.444362), ne: CLLocationCoordinate2D(latitude: 56.375322, longitude: -4.128659)), edgePadding: UIEdgeInsets.zero, animated: false)

To the same results as shown below. As you can see the iphone 5s layout is perfect but I'm expecting the code above to show the same view on ipad however it is zoomed out too much. How can I show the same view across devices?

IPad

Iphone 5s

3

3 Answers

6
votes

Ok I got this working. There is a camera method on the MGLMapView that will return a camera that zooms as far in as it can while still displaying a rectangle you specify. Just have to make sure this is done in the didFinishLoading delegate method.

let northeast = CLLocationCoordinate2D(latitude: 55.499130, longitude: -5.945938)
let southwest = CLLocationCoordinate2D(latitude: 51.133369, longitude: -10.531064)

let ireland = MGLCoordinateBounds(sw: southwest, ne: northeast)

let camera = mapView.camera(mapView.camera, fitting: ireland, edgePadding: UIEdgeInsets.zero)

mapView.setCamera(camera, animated: false)
2
votes

There is an example on Mapbox's website that does exactly (I think) what you want. I've taken the liberty of inserting your coordinates, etc. as below. Links break so I'll post the code.

EDIT: Changed to disallow scrolling, panning and zooming.

import Mapbox

class ViewController: UIViewController, MGLMapViewDelegate {

    private var ireland: MGLCoordinateBounds!

    override func viewDidLoad() {
        super.viewDidLoad()

        let mapView = MGLMapView(frame: view.bounds)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        mapView.delegate = self


        let northeast = CLLocationCoordinate2D(latitude: 56.375322, longitude: -4.128659)
        let southwest = CLLocationCoordinate2D(latitude: 50.347676, longitude: -11.444362)

        var box = [northeast, southwest]
        mapView.setVisibleCoordinates(&box, count: 2, edgePadding: UIEdgeInsets.init(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0), animated: false)
        mapView.isScrollEnabled = false
        mapView.allowsZooming = false

        view.addSubview(mapView)
    }
}
0
votes

This helps me to zoom particular region in Mapbox

self.mapView.centerCoordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
self.mapView.zoomLevel = 13
self.mapView.centerCoordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
self.mapView.setCenter(CLLocationCoordinate2D(latitude: lat, longitude: long, animated: true)