将地址转换为苹果地图 link 以与他人分享

Converting address to apple maps link to share with others

我想将地址转换成苹果地图 link 并将其发送给使用 UIActivityViewController 的人。我知道 CLGeocoder,我知道如何将字符串转换为坐标,反之亦然,但我不知道这有什么用。具体来说,我希望能够生成苹果地图 link 以便我可以与 UIActivityViewController 共享它。这是我目前拥有的 activityViewController 代码:

@IBAction func sendAddress(_ sender: UIButton) {
    // how to generate something like this link below, except in apple maps???
    let text = URL(string:"https://www.google.com/maps/@42.585444,13.007813,6z")
    
    // making activity view controller
    let textToShare = [ text ]
    let activityViewController = UIActivityViewController(activityItems: textToShare as [Any], applicationActivities: nil)
    activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
    
    // present
    self.present(activityViewController, animated: true, completion: nil)
    
}

我需要的只是如何根据坐标或地址创建 link,因为我很确定您可以将 URL 作为可点击的 link 与消息共享,因为我使用内置于 iOS 模拟器中的提醒对其进行了测试。

谢谢

你需要做两件事

  1. 获取当前位置
  2. 使用经纬度打开 UIActivityController

要获取当前位置的经纬度,您可以使用 CLCoordinate

首先将这些添加到您的 info.plist 您可以随意修改文本

 <key>NSLocationAlwaysUsageDescription</key>
<string>Will you allow this app to always know your location?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>

现在在你的 class 中创建一个 CLLocationManager 的对象并实现它的委托,因为 CLLocationManager 在 CoreLocation 中我们需要导入它

import CoreLocation

现在创建locationManager的对象

let locationManager = CLLocationManager()

现在在 viewDidload 中,或者您甚至可以创建一个单独的方法,将以下代码添加到设置 locationManager

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()

    if CLLocationManager.locationServicesEnabled(){
        locationManager.startUpdatingLocation()
    }

现在实现它的委托并获取坐标

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation :CLLocation = locations[0] as CLLocation
    let coordinates = userLocation!.coordinate
    print("locations = \(coordinates.latitude) \(coordinates.longitude)")
    }

现在你可以调用下面的函数来打开动作Sheet

if let shareObject = self.activityItems(latitude: lat, longitude: long) {
       //open UIActivityViewController 
}

使用此方法制作vCard分享

func activityItems(latitude: Double, longitude: Double) -> [AnyObject]? {
    var items = [AnyObject]()

    let locationTitle = "Shared Location"
    let URLString = "https://maps.apple.com?ll=\(latitude),\(longitude)"

    if let url = NSURL(string: URLString) {
        items.append(url)
    }

    let locationVCardString = [
        "BEGIN:VCARD",
        "VERSION:3.0",
        "PRODID:-//Joseph Duffy//Blog Post Example//EN",
        "N:;\(locationTitle);;;",
        "FN:\(locationTitle)",
        "item1.URL;type=pref:\(URLString)",
        "item1.X-ABLabel:map url",
        "END:VCARD"
        ].joinWithSeparator("\n")

    guard let vCardData = locationVCardString.dataUsingEncoding(NSUTF8StringEncoding) else {
        return nil
    }

    let vCardActivity = NSItemProvider(item: vCardData, typeIdentifier: kUTTypeVCard as String)

    items.append(vCardActivity)

    items.append(locationTitle)

    return items
}

参考link:https://josephduffy.co.uk/posts/ios-share-sheets-the-proper-way-locations