[英]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
.. 我曾经在函数中使用
mylat
, mylong
..但我想用全局变量添加用户最近的位置,并且在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.