MKMapItem.openInMaps () shows the elevation mark exactly 50% of the time

I found that running some code to display the location in the form of maps MKMapItem.openInMaps() works exactly 50% of the time.
In fact, it exactly alternates with the displayed MKPlacemark and is not displayed.

For example, every 1st, 3rd, 5th, 7th ... nth time the code is run, it displays a placemark, but every 2nd, 4th, 6th, 8 th ... at the time of its launch, the place mark is not displayed.

This is a 100% reproducible code posted below.
This is similar to his mistake, but if so, I am surprised that he was not reported and not fixed earlier. But given the fact that failures exactly alternate between success and failure, I make you think that something else is happening, so I am posting here to find out if anyone is familiar with this problem, or is there something that you need to do what is missing from the code, or there is a workaround:

 override func viewDidAppear(_ animated: Bool) { displayMap() } func displayMap() { let geoCoder = CLGeocoder() geoCoder.geocodeAddressString("1 Infinite Loop, Cupertino,California") { (placemark: [CLPlacemark]?, error: Error?) -> Void in if error == nil { if let placemark = placemark, placemark.count > 0 { let location = placemark.first let latitude = (location?.location?.coordinate.latitude)! let longitude = (location?.location?.coordinate.longitude)! let coordinates = CLLocationCoordinate2DMake(latitude, longitude) let regionDistance:CLLocationDistance = 100000 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 = "Apple" mapItem.phoneNumber = "(405) 123-4567" mapItem.openInMaps(launchOptions: options) } } else { assert(false, "Unable to geocode") } } } 

Here's what the result looks like when the code runs for the first, third, fifth, seventh ... time

Odd

And this is the result when the code is run for the second, fourth, sixth, eighth ... time

Even

Pay attention to the screenshot of the failure, which is not only not marked on the place map, but the slide up is also empty.

(Currently it is observed on 10.2, but saw other versions)

+6
source share
2 answers

Renouncement

Yes, there is an error in mapItem.openInMaps that opens driving directions as an overlay.

As accurately described by @ return0 :

... I tried your code, and the problem is that after you place it in the center and show, if you do not let it go and run the application again, it will not show ...

More oddities

  • The second time, mapItem.openInMaps cancels the overlapping of driving directions (instead shows a new location).
  • If the user closes the mentioned overlay, Map is no longer confused (*)

(*) Another oddity in this situation is that after closing this overlay, the location disappears.

Error? → Workaround!

Set up overlay directions to get away by requesting multiple locations. In practice, this means that you use Maps twice in the same place:

 MKMapItem.openMaps(with: [mapItem, mapItem], launchOptions: options) 

no directions, but not stuck directions but not stuck

Defensive bonus

Even if the user clicks on the anchor and asks for the direction of movement, subsequent access to the Maps from your application to this or another place will not leave it hanging. In other words, it works as expected.


Source Code Complete Swift 3

 func displayMap(_ address:String, name:String, tph:String) { let geoCoder = CLGeocoder() geoCoder.geocodeAddressString(address) { (placemark: [CLPlacemark]?, error: Error?) -> Void in assert(nil == error, "Unable to geocode \(error)") if error == nil { if let placemark = placemark, placemark.count > 0 { let location = placemark.first let latitude = (location?.location?.coordinate.latitude)! let longitude = (location?.location?.coordinate.longitude)! let coordinates = CLLocationCoordinate2DMake(latitude, longitude) let regionDistance:CLLocationDistance = 10000 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 = name mapItem.phoneNumber = tph MKMapItem.openMaps(with: [mapItem, mapItem], launchOptions: options) } else { print("Something wrong with \(placemark)") } } } } 

... and the challenge

 @IBAction func doApple() { displayMap("1 Infinite Loop, Cupertino, California", name: "Apple", tph: "(408) 996–1010") } @IBAction func doMicrosoft() { displayMap("One Microsoft Way, Redmond, WA", name: "Microsoft", tph: "1-800-MICROSOFT") } @IBAction func doIBM() { displayMap("1 New Orchard Road. Armonk, New York", name: "IBM", tph: "(914) 499-1900") } 
+2
source

In iOS 10.3.2, this question still persists for me. However, this workaround prevents mapItem from disappearing:

 MKMapItem.openMaps(with: [mapItem], launchOptions: options) 
0
source

Source: https://habr.com/ru/post/1013585/


All Articles