In objective C (I have no experience with it) you can initialize a CLLocation object with latitude and longitude likes this:
在目标C(我没有经验)你可以初始化纬度和经度的CLLocation对象喜欢这样:
CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:your_latitiude_value longitude:your_longitude_value];
But how do I do it in Swift?
但是我如何在Swift中做到这一点?
I tried this,
我试过这个,
let loc_coords = CLLocationCoordinate2D(latitude: your_latitiude_value, longitude: your_longitude_value)
let loc = CLLocation().coordinate(loc_coords)
But I get this error:
但我得到这个错误:
Cannot invoke "coordinate" with an argument list of type '(CLLocationCoordiate2D)'
无法使用类型'(CLLocationCoordiate2D)'的参数列表调用“coordinate”
I need it to be a CLLocation object as I want to use the distanceFromLocation method within the locationManager.
我需要它作为CLLocation对象,因为我想在locationManager中使用distanceFromLocation方法。
Hope you can help, thanks!
希望你能帮忙,谢谢!
2 个解决方案
#1
22
I am not near my Mac to test it but the following should be working. Just pass your lat and lon values to the CLLocaion initializer like this:
我不是靠近我的Mac测试它,但以下应该工作。只需将lat和lon值传递给CLLocaion初始值设定项,如下所示:
let myLocation = CLLocation(latitude: your_latitiude_value, longitude: your_longitude_value)
For reference you can check the documentation
供参考,您可以查看文档
#2
9
If all you want is the initializer that takes a lat and long, your code can look like this:
如果你想要的只是一个lat和long的初始化器,你的代码可能如下所示:
let latitude: CLLocationDegrees = 37.2
let longitude: CLLocationDegrees = 22.9
let location: CLLocation = CLLocation(latitude: latitude,
longitude: longitude)
There are other initializers that take more parameters, but you didn't ask about those.
还有其他初始化程序需要更多参数,但您没有询问这些参数。
#1
22
I am not near my Mac to test it but the following should be working. Just pass your lat and lon values to the CLLocaion initializer like this:
我不是靠近我的Mac测试它,但以下应该工作。只需将lat和lon值传递给CLLocaion初始值设定项,如下所示:
let myLocation = CLLocation(latitude: your_latitiude_value, longitude: your_longitude_value)
For reference you can check the documentation
供参考,您可以查看文档
#2
9
If all you want is the initializer that takes a lat and long, your code can look like this:
如果你想要的只是一个lat和long的初始化器,你的代码可能如下所示:
let latitude: CLLocationDegrees = 37.2
let longitude: CLLocationDegrees = 22.9
let location: CLLocation = CLLocation(latitude: latitude,
longitude: longitude)
There are other initializers that take more parameters, but you didn't ask about those.
还有其他初始化程序需要更多参数,但您没有询问这些参数。