簡體   English   中英

如何在viewdidload()中使用CLLocation的位置(緯度,經度)

[英]how to use my location(latitude,longitude) of CLLocation at viewdidload()

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


        let userLocation: CLLocation = locations[0]

        let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)

        mylat = userLocation.coordinate.latitude
        mylong = userLocation.coordinate.longitude

        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.04, longitudeDelta: 0.04))

        totalMap.setRegion(region, animated: true)
}

首先..我為我的英語寫作感到抱歉...

我曾經在函數中使用mylatmylong ..但我想用全局變量添加用戶最近的位置,並且在viewDidLoad使用該用戶位置。

實際上,我需要用戶location(lat,long)和許多其他位置,然后我將比較操作與兩個位置進行比較,而我幾乎需要大部分位置。

幫我

CLLocationManager異步工作。 您可以在viewDidLoad()使用,但不能像您想象的那樣使用。

您可以這樣注冊一個回調:

var onLocationUpdate: ((CLLocation) -> ())?
override func viewDidLoad() {
    super.viewDidLoad()
    onLocationUpdate = { [weak self] location in
        // do something with the location
    }
}

並在locationManager(_:didUpdateLocations:)方法中調用onLocationUpdate?(YOUR_LOCATION)

暫無
暫無

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

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