簡體   English   中英

將地址轉換為蘋果地圖鏈接以與他人分享

[英]Converting address to apple maps link to share with others

我想將地址轉換為蘋果地圖鏈接並將其發送給具有 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 模擬器中內置的提醒對其進行了測試。

謝謝

你需要做兩件事

  1. 獲取當前位置
  2. 使用 lat, long 打開 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>

現在在你的類中創建一個 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM