如何在 iOS 上的变量中获取位置(纬度和经度值)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12736086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Location (latitude & longitude value) in variable on iOS?
提问by Laurenz Glück
I would like to use the location of my app user, more specifically the latitude and longitude value.
我想使用我的应用程序用户的位置,更具体地说是纬度和经度值。
I need them to use in a variable, so that I can send them with an url...
我需要它们在变量中使用,以便我可以使用 url 发送它们...
I never worked before withe locations on iOS and Xcode and found nothing matching for my problem on the Internet...
我以前从未在 iOS 和 Xcode 上使用过位置,并且在 Internet 上找不到与我的问题匹配的任何内容...
So I hope someone can explain it to my with a code sample :)
所以我希望有人可以用代码示例向我解释它:)
Thx for u help Laurenz from Germany
谢谢你帮助来自德国的 Laurenz
回答by Peter Zhou
You can use CoreLocation to get the longitude and latitude.
您可以使用 CoreLocation 来获取经度和纬度。
Include framework:
包括框架:
Click your project in navigator.
在导航器中单击您的项目。
Click the plus button under "link binary with libraries"
单击“链接二进制与库”下的加号按钮
Add Corelocation to your project.
将 Corelocation 添加到您的项目中。
Import the header file:
导入头文件:
#import <CoreLocation/CoreLocation.h>
Declare CLLocationManager:
声明 CLLocationManager:
CLLocationManager *locationManager;
initialize locationManager:
初始化位置管理器:
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
Then, use
然后,使用
float latitude = locationManager.location.coordinate.latitude;
float longitude = locationManager.location.coordinate.longitude;
回答by Jayprakash Dubey
STEP 1 : Add CoreLocation framework in your project.
第 1 步:在您的项目中添加 CoreLocation 框架。
- Select TARGETS-> Build Phases
- Select Link Binary With Libraries
- Click on plus (+)button. This will pop-up framework list.
- Search CoreLocation frameworkand Addit.
- 选择目标->构建阶段
- 选择链接二进制与库
- 单击加号(+)按钮。这将弹出框架列表。
- 搜索CoreLocation 框架并添加它。
STEP 2 : Write below code in header file of view controller where location is to be fetched :
第 2 步:在要获取位置的视图控制器的头文件中写入以下代码:
#import <CoreLocation/CoreLocation.h>
Also, add CLLocationManagerDelegate
in interface.
另外,CLLocationManagerDelegate
在接口中添加。
Now, create object of LocationManager
现在,创建 LocationManager 的对象
CLLocationManager *locationManager;
STEP 3 : In, ViewDidLoad
method write below code :
第 3 步:在,ViewDidLoad
方法写下面的代码:
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
[locationManager requestAlwaysAuthorization]; //Note this one
You can set desiredAccuracy value as kCLLocationAccuracyBest, kCLLocationAccuracyNearestTenMeters, kCLLocationAccuracyHundredMeters, kCLLocationAccuracyKilometer, kCLLocationAccuracyThreeKilometers
您可以将 desiredAccuracy 值设置为 kCLLocationAccuracyBest, kCLLocationAccuracyNearestTenMeters, kCLLocationAccuracyHundredMeters, kCLLocationAccuracyKilometer, kCLLocationAccuracyThreeKilometers
Now, write below code to get latitude and longitude value
现在,编写下面的代码来获取纬度和经度值
float Lat = locationManager.location.coordinate.latitude;
float Long = locationManager.location.coordinate.longitude;
NSLog(@"Lat : %f Long : %f",Lat,Long);
STEP 4 : In iOS 8, this code fails silentlyi.e you won't get any error or warning.
第 4 步:在 iOS 8 中,此代码会静默失败,即您不会收到任何错误或警告。
You need to do two extra things to get location working:
您需要做两件额外的事情才能使定位正常工作:
- Add a key to your Info.plist
- Request authorizationfrom the location manager asking it to start.
- 向您的 Info.plist 添加密钥
- 向位置管理器请求授权,要求它启动。
Any one or both of below keys needs to be added in Info.plist file.
需要在 Info.plist 文件中添加以下任何一个或两个键。
- NSLocationWhenInUseUsageDescription
- NSLocationAlwaysUsageDescription
- NSLocationWhenInUseUsageDescription
- NSLocationAlwaysUsageDescription
These are of String type and value can be any message description or empty.
这些是字符串类型,值可以是任何消息描述或为空。
Now you need to request authorisation for corresponding location method. Use any one of below calls :
现在您需要为相应的定位方法请求授权。使用以下任一调用:
- [self.locationManager requestWhenInUseAuthorization]
- [self.locationManager requestAlwaysAuthorization]
- [self.locationManager requestWhenInUseAuthorization]
- [self.locationManager requestAlwaysAuthorization]
回答by linker
Swift:
迅速:
var locationManager = CLLocationManager()
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.startUpdatingLocation()
更多信息:https: //developer.apple.com/library/mac/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/cl/CLLocationManager