ios - MKMapItem.openInMaps() 准确显示位置标记的时间为 50%

标签 ios apple-maps mkmapitem

我发现运行一些代码以在 map View 中显示位置 MKMapItem.openInMaps() 只有 50% 的时间有效。
事实上,它在显示和不显示 MKPlacemark 之间精确交替。

例如,代码每运行 1 次、3 次、5 次、7 次……第 n 次,它就会显示位置标记,但每运行 2 次、4 次、6 次、8 次……第 m 次,就不会显示位置标记显示。

运行下面发布的代码可 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")
        }
    }
}

这是第一次、第三次、第五次、第七次……运行代码时的结果

Odd

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

Even

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

(目前在 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. 如果用户关闭所述叠加层, map 将不再混淆(*)

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

错误? → 解决方法!

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

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

no directions, but not stuck directions but not stuck

变通奖励

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


完整的 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)")
            }
        }
    }
}

...和调用

@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 - MKMapItem.openInMaps() 准确显示位置标记的时间为 50%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41369791/

相关文章:

android - 适用:iOS 7.1 支持 MKMapItem

ios - 从 iOS 中 UITableView 中的单元格中分离

ios - boolean 返回帮助查找修复

iOS 如何显示用户正在走向哪个方向 Swift

iOS MKMapItem - 如何访问所有成员?

ios - 在核心数据中存储 CLLocation 坐标

ios - 将 View 约束到 UIBarButtonItem 的位置

ios - xCode 4.3.2 链接错误,但 arch 确实存在。为什么?

ios - 自定义 didSelectAnnotationView 不在 map 中滚动

google-maps - Xamarin.Forms map 图钉