簡體   English   中英

如何使用CoreLocation在當前位置添加注釋

[英]How to Add an annotation At your current location using CoreLocation

除了實際的按鈕功能代碼之外,我所有的工作都可以進行。 這是我的代碼,但未添加注釋。

@IBAction func AddAnnotation(_ sender: UIButton) {
        CLLocationManager().startUpdatingLocation()
        let annotation = MKPointAnnotation()
        // Set the annotation by the lat and long variables
        annotation.coordinate = CLLocationCoordinate2D()
        annotation.title = "My Car"
        self.map.addAnnotation(annotation)
    }

您的代碼可能無法正常工作。 當您調用startUpdatingLocation() ,GPS會花費幾秒鍾來啟動並獲得准確的定位。 您必須將自己設置為委托,然后等待您的locationManager(_:didUpdateLocations:)方法被調用。

您需要將代碼放入該方法中添加注釋。

正如其他人所說,您還需要編寫代碼以檢查您是否被授權獲取用戶的位置,如果沒有,則請求權限。 (並且,您需要在應用的info.plist文件中添加一個特殊鍵,以指示您的應用需要訪問用戶的位置。)

查看有關Core Location的文檔,並在Xcode中使用location Manager,您可能想要找到一個指導您完成這些步驟的教程。 這有點涉及。

暫無
暫無

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

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