【问题标题】:Open Apple Maps programmatically以编程方式打开 Apple 地图
【发布时间】:2016-02-20 15:17:18
【问题描述】:

我想在自己的 Swift 应用中打开 Apple 地图应用,但我只有邮政编码、城市和街道。我没有坐标。我研究了很多,但只有使用协调信息的方法。

【问题讨论】:

标签: ios swift iphone apple-maps


【解决方案1】:

您可以在打开地图应用程序的 URL 中将地址信息作为 URL 参数传递。假设您希望地图应用以白宫为中心打开。

UIApplication.sharedApplication().openURL(NSURL(string: "http://maps.apple.com/?address=1600,PennsylvaniaAve.,20500")!)

地图应用程序打开时搜索字段中的查询字符串很丑,但它显示了正确的位置。请注意,搜索查询中没有城市和州,它只是街道地址和邮政编码。

根据您的需要,一种可能更好的方法是使用 CLGeocoder 获取您拥有的地址信息的 CLLocation。

let geocoder = CLGeocoder()
let str = "1600 Pennsylvania Ave. 20500" // A string of the address info you already have
geocoder.geocodeAddressString(str) { (placemarksOptional, error) -> Void in
  if let placemarks = placemarksOptional {
    print("placemark| \(placemarks.first)")
    if let location = placemarks.first?.location {
      let query = "?ll=\(location.coordinate.latitude),\(location.coordinate.longitude)"
      let path = "http://maps.apple.com/" + query
      if let url = NSURL(string: path) {
        UIApplication.sharedApplication().openURL(url)
      } else {
        // Could not construct url. Handle error.
      }
    } else {
      // Could not get a location from the geocode request. Handle error.
    }
  } else {
    // Didn't get any placemarks. Handle error.
  }
}

【讨论】:

  • 您可以使用 %20 代替空格,而不是使用逗号分隔字符串中的单词,因此例如“1600%20Pennsylvania%20Ave.%2020500”将适用于您的答案的第一个选项和将在“1600 Pennsylvania Ave. 20500”等地图中打开
  • +1 @JCode,查询应该被“编码”,以便正确解析空格和其他特殊字符
【解决方案2】:

Swift 4 及以上

使用这个版本获取地址的坐标,也有直接用地址打开的方法,但是容易出错

import CoreLocation

let myAddress = "One,Apple+Park+Way,Cupertino,CA,95014,USA"
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(myAddress) { (placemarks, error) in
    guard let placemarks = placemarks?.first else { return }
    let location = placemarks.location?.coordinate ?? CLLocationCoordinate2D()
    guard let url = URL(string:"http://maps.apple.com/?daddr=\(location.latitude),\(location.longitude)") else { return }
    UIApplication.shared.open(url)
}

Apple 有关于 Map URL Scheme 的文档。看这里:https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html#//apple_ref/doc/uid/TP40007899-CH5-SW1

【讨论】:

    【解决方案3】:

    使用 Swift 4 和 Xcode 9

    在顶部:

    import CoreLocation
    

    然后:

    let geocoder = CLGeocoder()
    
    let locationString = "London"
    
    geocoder.geocodeAddressString(locationString) { (placemarks, error) in
        if let error = error {
            print(error.localizedDescription)
        } else {
            if let location = placemarks?.first?.location {
                let query = "?ll=\(location.coordinate.latitude),\(location.coordinate.longitude)"
                let urlString = "http://maps.apple.com/".appending(query)
                if let url = URL(string: urlString) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      相关资源
      最近更新 更多