MKMapItem.openInMaps() 恰好有 50% 的时间显示地点标记

MKMapItem.openInMaps() displays place mark exactly 50% of the time

我发现运行使用一些代码在地图视图中显示位置 MKMapItem.openInMaps() 仅在 50% 的时间内有效。
事实上,它在 MKPlacemark 显示和不显示之间精确交替。

例如,代码每第 1、3、5、7 ...n 次显示位置标记 运行s,但每 2、4、6、8 ...第 m 次显示位置标记运行s,地标未显示

这是 100% 可重现的运行使用下面发布的代码。
这似乎是一个错误,但如果是这样,我很惊讶它之前没有被报告或修复。但鉴于失败恰恰在成功和失败之间交替,这让我认为还有其他事情正在发生,因此我在这里发帖是为了看看是否有人熟悉这个问题,或者有人应该做的事情缺失了从代码中,或者有一个解决方法:

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")
        }
    }
}

这是当代码为 运行 第一、第三、第五、第七 ... 时间

时的结果

这是代码为运行第二次、第四次、第六次、第八次...时的结果

注意失败截图中不仅地图上没有显示地标,而且上滑也是空的

(目前在 10.2 上观察到这一点,但也在其他版本上看到过)

免责声明

是的,似乎mapItem.openInMaps 中的错误,它以叠加层形式打开行车路线。

正如 @return0 准确描述的那样:

...I tried your code and the problem lies in the fact that once you have the location centered and showing, if you don't dismiss it and launch the app again it will not show...

更多怪事

  1. 第二次,mapItem.openInMaps 确实关闭行车路线叠加层(而不是显示新位置)
  2. 如果用户关闭所述叠加层,地图将不再混乱(*)

(*) 这种情况的另一个奇怪之处在于,一旦覆盖层关闭,该位置就会消失。

错误? → 解决方法!

通过请求多个位置强制关闭路线叠加层。实际上,这意味着调用具有两次相同位置的地图:

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

解决方案奖金

即使用户点击锚点并请求行车路线,随后从您的应用程序调用地图到该位置或其他位置也不会不会让它挂起。换句话说,它按预期工作。


完成Swift3源代码

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)")
            }
        }
    }
}

...和调用

@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")
}

在 iOS 10.3.2 上,这个问题对我来说仍然存在。但是,此解决方法使 mapItem 不会消失:

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