为什么纬度和经度在ios 9中显示为零?

时间:2016-05-31 06:18:41

标签: ios objective-c cllocationmanager

我必须找到当前位置,我正在使用此代码

 -(CLLocationCoordinate2D) getLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;

}

- (void)getCurrentLocation{
    CLLocationCoordinate2D coordinate = [self getLocation];
    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
    NSLog(@"Latitude  = %@", latitude);
    NSLog(@"Longitude = %@", longitude);
}

但纬度和经度是零?我在viewDidLoad

中调用了这些方法
[self getCurrentLocation];
[self getLocation];

为什么这样请帮助我。感谢

6 个答案:

答案 0 :(得分:3)

我希望,以下信息可以帮助您:

 #import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface yourController : UIViewController <CLLocationManagerDelegate> {
    CLLocationManager *locationManager;
}

@end

并添加:

    locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    [self.locationManager requestWhenInUseAuthorization];

[locationManager startUpdatingLocation];

回调功能

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}

您还必须为

添加字符串
[`NSLocationAlwaysUsageDescription`]

答案 1 :(得分:2)

您需要使用“CLLocationManagerDelegate”。

请完成本教程。 How To Get the User Location in iPhone App

在这里,您将了解CoreLocation Framework

这是委托方法,它将返回当前的lat-long。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

并确保您的应用授权使用位置服务。

并且还在设备上运行您的应用,或者如果您想在模拟器中查看结果,请按照教程中提到的步骤操作。

第1步#import <CoreLocation/CoreLocation.h>导入框架。还要在项目中添加框架。

第2步:确保您的视图控制器实现CLLocationManagerDelegate

@interface MyLocationViewController : UIViewController <CLLocationManagerDelegate>

第3步:定义CLLocationManager

的类实例
@implementation MyLocationViewController {
    CLLocationManager *locationManager;
}

第4步

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    locationManager = [[CLLocationManager alloc] init];

    if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            //iOS 8.0 onwards
            [locationManager requestAlwaysAuthorization];
        }
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
}

第5步:这是您的CLLocationManagerDelegate  方法实现

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                                    initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
       NSLog(@"Longitude %.8f",currentLocation.coordinate.longitude);
       NSLog(@"Latitude %.8f",currentLocation.coordinate.latitude);
    }
}

答案 2 :(得分:1)

首先转到info.plist并右键单击它并选择源代码选项。 enter image description here

然后在<dict>关键字

之后添加以下行
    <key>NSLocationAlwaysUsageDescription</key>
    <string>This App wants to Know your location</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>This App wants to Know your location</string>

如果没有这个,你就无法在iOS 9.0中使用CLLocation。

现在按照说明进行操作。

导入

#import <CoreLocation/CoreLocation.h>

并列出CoreLocation委托,如

@interface ViewController ()<CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;

@end

然后在viewDidLoad方法中添加以下行 -

    self.locationManager = [[CLLocationManager alloc]init];
    self.locationManager.delegate=self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
        [self.locationManager requestWhenInUseAuthorization];
    }
    [self.locationManager startUpdatingLocation];

现在实现委托方法

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Sorry" 
                               message:@"Failed to Get Your Location"      
                               delegate:nil 
                               cancelButtonTitle:@"OK"  
                               otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        NSLog(@"%@", [NSString stringWithFormat:@"%.4f", currentLocation.coordinate.longitude]);
        NSLog(@"%@", [NSString stringWithFormat:@"%.4f", currentLocation.coordinate.latitude]);
    }
}

答案 3 :(得分:0)

CLLocationManager获取coordinates需要一些时间。因此,CLLocationManager使用委托在收到位置更新时通知。因此,您无法立即获得结果。请查看this帖子,看看它是如何完成的。

答案 4 :(得分:0)

好吧,我希望你阅读doc,我仍在发布解决方案。

-(void)viewDidLoad{
     [self initLocationManager];
}


-(void)initLocationManager{

    self.locationManager=[[CLLocationManager alloc] init];
    self.locationManager.delegate=self;
    if (([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
        [_locationManager requestWhenInUseAuthorization];
    }
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    [self.locationManager startUpdatingLocation];

}

然后在CLLocationManager委托方法中处理位置更新,如下所示

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

    // If it's a relatively recent event, turn off updates to save power.
    CLLocation* location = [locations lastObject];
    NSDate* eventDate = location.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0) {


        NSLog(@"%f,%f",location.coordinate.latitude,location.coordinate.longitude);


    }
}

<强>更新

要使其有效,您需要做两件事

  1. Background Modes标签启用Capabilities,然后选择Location Updates,请参阅下面的图片

    enter image description here

  2. 在信息标签中添加NSLocationWhenInUseUsageDescription到plist中,参见图片

    enter image description here

  3. 它将开始工作。

    希望它有所帮助。欢呼声。

答案 5 :(得分:0)

使用此代码可以使用

首先在NSLocationAlwaysUsageDescription

中添加info.plist字符串

in .h

#import <CoreLocation/CoreLocation.h>

@interface ExamListVC : UIViewController<CLLocationManagerDelegate>
{
    CLLocationManager * locationManager;
}
@end
.m中的

使用此代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    locationManager . delegate = self;

    [self initLocationManager];
}

    -(void)initLocationManager
    {
        locationManager=[[CLLocationManager alloc] init];
        locationManager.delegate=self;
        if (([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]))
        {
            [locationManager requestWhenInUseAuthorization];
        }
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLDistanceFilterNone;
        [locationManager startUpdatingLocation];

    }


    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        CLLocation* location = [locations lastObject];
        NSDate* eventDate = location.timestamp;
        NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
        if (abs(howRecent) < 15.0)

        {
            NSLog(@"Current Coordinates are  %f,%f",location.coordinate.latitude,location.coordinate.longitude);
            [locationManager stopUpdatingLocation];
            }
        }

它会提供类似

的输出
  

[2390:96060]当前坐标为37.332331,-122.031219