【问题标题】:Converting address to apple maps link to share with others将地址转换为苹果地图链接以与他人分享
【发布时间】:2021-09-22 17:15:21
【问题描述】:

我想将地址转换为苹果地图链接并将其发送给具有 UIActivityViewController 的人。我知道 CLGeocoder,我知道如何将字符串转换为坐标,反之亦然,但我不知道这会有什么用。具体来说,我希望能够生成苹果地图链接,以便我可以与 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)
    
}

我所需要的只是如何从坐标或地址创建链接,因为我很确定您可以将 URL 共享为带有消息的可点击链接,因为我使用 iOS 模拟器中内置的提醒对其进行了测试.

谢谢

【问题讨论】:

    标签: ios swift uiactivityviewcontroller apple-maps


    【解决方案1】:

    你需要做两件事

    1. 获取当前位置
    2. 使用 lat、long 打开 UIActivityController

    要获取当前位置为 lat, long 可以使用 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>
    

    现在在你的类中创建一个 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)")
        }
    
    

    现在您可以调用以下函数来打开操作表

    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
    }
    

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

    【讨论】:

    • 非常感谢您的回答,不过我想知道,有没有办法在打开链接时在地址上添加注释?对我来说,地图只是以该位置为中心,但我希望有一个注释,以及底部的“显示方向”按钮。这可能吗?
    • @AlexAghajanov 是的,可以这样做,因为它与现有问题不同,我建议您根据该要求创建一个新问题。
    • 另一个问题已经问过了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 2012-10-15
    相关资源
    最近更新 更多