如何使用 UIActivityViewController 共享经度和纬度

How to share longitude and latitude using UIActivityViewController

我正在尝试在 UIActivityViewController 中分享与您的位置相关联的(经度、纬度),以便用户可以在短信中与其他人分享位置,它显示为可点击的小地图,如下所示.

我知道如何以文本形式分享地址。这是我分享地址的代码:

    @IBAction func didTapShareLocation(_ sender: UIButton) {
    guard let carAddress = self.adressLabel.text else {
        return
    }
    let textToShare = "My car is at this address: \(carAddress)"

    let objectsToShare = [textToShare] as [Any]
    let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

    activityVC.popoverPresentationController?.sourceView = sender
    myParentVC?.present(activityVC, animated: true, completion: nil)

}

这里是Swift 3.1 中的完整答案,是我从几个地方获取信息后整理出来的。我希望它对某人有所帮助。

    @IBAction func didTapShareLocation(_ sender: UIButton) {
    guard let carAddress = self.adressLabel.text, let lat = self.carCoordinates?.latitude, let lon = self.carCoordinates?.longitude else {
        return
    }

    guard CLLocationCoordinate2DIsValid(self.carCoordinates!)  else {
        print("Location not valid!")
        return
    }

    let carAddressString = "My car is at this address: \n\(carAddress)\n"

    let vcardString = [
        "BEGIN:VCARD",
        "VERSION:3.0",
        "N:;Shared Location;;;",
        "FN:Shared Location",
        "item1.URL;type=pref:http://maps.apple.com/?ll=\(lat),\(lon)",
        "item1.X-ABLabel:map url",
        "END:VCARD"
        ].joined(separator: "\n")

    let directory = FileManager().urls(for: .cachesDirectory, in: .userDomainMask)

    let path = directory.first!.path + "_vcard_for_location_sharing.loc.vcf"
    do {
        try vcardString.write(toFile: path, atomically: true, encoding: .ascii)
        let url = NSURL(fileURLWithPath: path)

        let objectsToShare = [url, carAddressString] as [Any]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

        activityVC.popoverPresentationController?.sourceView = sender
        self.present(activityVC, animated: true, completion: nil)

    }
    catch   {
        print("problem saving vcard: \(error.localizedDescription)")
    }
}