我一直在尝试获取当前位置IOS8并跟随线程Location Services not working in iOS 8但是获得00.00,00.00而不是当前位置。关于我在哪里做错的任何建议。
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
NSLog(@"%@", [self deviceLocation]);
CLLocation *location = [self.locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSLog(@"%@",latitude);
NSLog(@"%@",longitude);
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)setUpMap
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
// Use one or the other, not both. Depending on what you put in info.plist
[self.locationManager requestAlwaysAuthorization];
}
#endif
[self.locationManager startUpdatingLocation];
}
-(NSString *)deviceLocation
{
return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[manager stopUpdatingLocation];
CLLocationCoordinate2D coordinate_currentlocation = [newLocation coordinate];
float latitude_current = newLocation.coordinate.latitude;
float longitude_current = newLocation.coordinate.longitude;
NSLog(@"%f",latitude_current);
NSLog(@"%f",longitude_current);
NSLog(@"Current latitude :%f",coordinate_currentlocation.latitude);
NSLog(@"Current longitude :%f",coordinate_currentlocation.longitude);
}
@end
我还在info.plist中添加了以下密钥
<key>NSLocationAlwaysUsageDescription</key>
<string>Your location is needed for this app.</string>
答案 0 :(得分:0)
在InfoPlist.strings文件中添加此字符串
1) NSLocationWhenInUseUsageDescription
2) NSLocationAlwaysUsageDescription
试试此代码
locationManagerApp=[[CLLocationManager alloc] init];
locationManagerApp.delegate = self;
locationManagerApp.distanceFilter = kCLDistanceFilterNone;
locationManagerApp.desiredAccuracy = kCLLocationAccuracyHundredMeters;
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[locationManagerApp requestAlwaysAuthorization];
}
[locationManagerApp startUpdatingLocation];
CLLocation *location1 = [locationManagerApp location];
CLLocationCoordinate2D coordinate = [location1 coordinate];
self.latValue= [NSString stringWithFormat:@"%f", coordinate.latitude];
self.longValue = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSLog(@"Latitude = %@", self.latValue);
NSLog(@"Longitude = %@", self.longValue);
在设备中运行您的项目。如果你在模拟器中运行项目,那么就不会得到lat / long。
获取模拟器上的当前位置(选择任何一个位置)。
答案 1 :(得分:0)
代码中存在错误,实际检查授权的函数从未调用过。我将所有代码移动到viewDidLoad,如下所示。感谢您对此进行调查。
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER)
{
[self.locationManager requestAlwaysAuthorization];
}
#endif
[self.locationManager startUpdatingLocation];
[self.locationManager startUpdatingLocation];
NSLog(@"%@", [self deviceLocation]);
[super viewDidLoad];
}