简体   繁体   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)
}

first.. i'm sorry for my english writning... 首先..我为我的英语写作感到抱歉...

i used to mylat , mylong in that func .. but i want to add user recently location with global variable and i use that user location in viewDidLoad .. 我曾经在函数中使用mylatmylong ..但我想用全局变量添加用户最近的位置,并且在viewDidLoad使用该用户位置。

actually i need user location(lat,long) and many other location, and then i use comparing opration with two location, and i need to most of nearly location .. 实际上,我需要用户location(lat,long)和许多其他位置,然后我将比较操作与两个位置进行比较,而我几乎需要大部分位置。

help me 帮我

The CLLocationManager works asynchronously. CLLocationManager异步工作。 You can use in your viewDidLoad() but not as you might think. 您可以在viewDidLoad()使用,但不能像您想象的那样使用。

You can register a callback like this: 您可以这样注册一个回调:

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

and call the onLocationUpdate?(YOUR_LOCATION) in the locationManager(_:didUpdateLocations:) method 并在locationManager(_:didUpdateLocations:)方法中调用onLocationUpdate?(YOUR_LOCATION)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 4 CLLocation用户位置经度和纬度 - Swift 4 CLLocation User Location Longitude & Latitude 如何从CLLocation变量中提取经度和纬度? - How to extract the longitude and the latitude from a CLLocation variable? 如何仅从CLLocation中获取纬度和经度值,而不是快速获取完整的位置详细信息 - How to take only Latitude and Longitude values from CLLocation instead of taking full location details in swift CLLocation:经度和纬度注册为0.000 - CLLocation: Longitude and Latitude registering as 0.000 iOS CLLocation - 获取ViewDidLoad上的位置 - iOS CLLocation - getting the location on ViewDidLoad iOS:使用纬度,经度,速度和时间戳创建CLLocation - iOS: Create CLLocation with latitude, longitude, speed and timestamp 获取用户的经度/纬度当viewDidLoad时 - Getting Longitude/Latitude of the User When the viewDidLoad 如何将快速CLlocation纬度和经度传递给wkwebview ajax函数或控制器函数 - How do I pass swift CLlocation latitude and longitude to wkwebview ajax function or controller function CLLocation接受经度和纬度大于或小于其最小值和最大值 - CLLocation accepts longitude and latitude greater or less than their min and max values 经度纬度点的奇怪位置 - Weird location of point with longitude latitude
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM