问题
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...
I never worked before withe locations on iOS and Xcode and found nothing matching for my problem on the Internet...
So I hope someone can explain it to my with a code sample :)
Thx for u help Laurenz from Germany
回答1:
You can use CoreLocation to get the longitude and latitude.
Include framework:
Click your project in navigator.
Click the plus button under "link binary with libraries"
Add Corelocation to your project.
Import the header file:
#import <CoreLocation/CoreLocation.h>
Declare 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;
回答2:
STEP 1 : Add CoreLocation framework in your project.
- Select TARGETS -> Build Phases
- Select Link Binary With Libraries
- Click on plus (+) button. This will pop-up framework list.
- Search CoreLocation framework and Add it.
STEP 2 : Write below code in header file of view controller where location is to be fetched :
#import <CoreLocation/CoreLocation.h>
Also, add CLLocationManagerDelegate in interface.
Now, create object of LocationManager
CLLocationManager *locationManager;
STEP 3 : In, ViewDidLoad method write below code :
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
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 silently i.e you won’t get any error or warning.
You need to do two extra things to get location working:
- Add a key to your Info.plist
- Request authorization from the location manager asking it to start.
Any one or both of below keys needs to be added in Info.plist file.
- 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]
回答3:
Swift:
var locationManager = CLLocationManager()
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.startUpdatingLocation()
More info here: https://developer.apple.com/library/mac/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/cl/CLLocationManager
来源:https://stackoverflow.com/questions/12736086/how-to-get-location-latitude-longitude-value-in-variable-on-ios