Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openInMapsWithLaunchOptions not working?

Tags:

swift

ios8

mapkit

I'm passing in options for the map, but this does not seem to do anything with the zoom level?? It keeps the same low zoom level. What have I missed?

func openMapForPlace() {
    let regionDistance:CLLocationDistance = 10000
    var coordinates = CLLocationCoordinate2DMake(detailItem!.geoLatitude, detailItem!.geoLongitude)
    let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
    var options = [
        MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
        MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
    ]
    var placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
    var mapItem = MKMapItem(placemark: placemark)
    mapItem.name = detailItem!.cityName
    mapItem.openInMapsWithLaunchOptions(options)
}
like image 706
Bjarte Avatar asked Feb 10 '15 08:02

Bjarte


1 Answers

Apple's documentation does not mention it but from testing, it seems like openInMapsWithLaunchOptions() seems to ignore the MKLaunchOptionsMapSpanKey option if one or more MKMapItem are added to the map.

The following code works as expected, with the map zoom being adjusted properly when the distance parameter is modified (try with 1000 and 10000000, to see the difference):

func openMapForPlace() {
    let regionDistance: CLLocationDistance = 10000000
    let coordinates = CLLocationCoordinate2DMake(40, 0)
    let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
    let options = [
        MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
        MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
    ]

    MKMapItem.openMapsWithItems([], launchOptions: options)
}

However, as soon as one MKMapItem is added to the map, the zoom stops working.

func openMapForPlace() {
    let regionDistance: CLLocationDistance = 10000000
    let coordinates = CLLocationCoordinate2DMake(40, 0)
    let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
    let options = [
        MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
        MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
    ]

    let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = "Test"

    MKMapItem.openMapsWithItems([mapItem], launchOptions: options)
}
like image 168
Eneko Alonso Avatar answered Oct 21 '22 13:10

Eneko Alonso