我没有得到我的位置

时间:2015-07-28 04:20:39

标签: ios objective-c iphone

我没有在我的项目中获得我的位置。我的代表没有打电话。当我在项目中使用他们不工作。当我在其他工作中使用demo时。我是iOS的新手。请帮助。

    self.locationManager = [[CLLocationManager alloc] init];

    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestAlwaysAuthorization];

    self.locationManager.delegate = self;

    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.

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

        [self.locationManager requestWhenInUseAuthorization];

    }

    [self.locationManager startUpdatingLocation];


}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{



    CLLocation *currentLocation = [locations lastObject];

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

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



}

- (void)requestAlwaysAuthorization

{

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];



    // If the status is denied or only granted for when in use, display an alert

    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) {

        NSString *title;

        title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";

        NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";



        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title

                                                            message:message

                                                           delegate:self

                                                  cancelButtonTitle:@"Cancel"

                                                  otherButtonTitles:@"Settings", nil];

        [alertView show];

    }

    // The user has not enabled any location services. Request background authorization.

    else if (status == kCLAuthorizationStatusNotDetermined) {

        [self.locationManager requestAlwaysAuthorization];

    }

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

{

    NSLog(@"didUpdateToLocation: %@", newLocation);

    CLLocation *currentLocation = newLocation;


    if (currentLocation != nil) {

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

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



    }

}

3 个答案:

答案 0 :(得分:1)

我想您忘记在plist中添加位置管理器密钥了。在.plist文件中添加以下键

 1.  NSLocationAlwaysUsageDescription
 2.  NSLocationWhenInUseUsageDescription

希望它能起作用......

答案 1 :(得分:0)

首先导入以下框架。

 #import <CoreLocation/CoreLocation.h>

以及在控制器中添加委托。

CLLocationManagerDelegate

答案 2 :(得分:0)

您需要检查的一些事项。

1.在项目plist中设置位置键,只需选择一个并设置描述:

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

2.检查您的代表是否正确连接。

3.如果您在模拟器中运行应用程序,则需要从调试菜单中选择一个位置服务的位置才能工作。

4.检查您运行应用程序的任何设备是否可以实际获取位置数据。例如,iPod需要连接到wifi才能知道它们的位置。

你还没有真正指出实际发生的事情。 “不起作用”不够具有描述性。

您发布的代码也可能会在iOS 7设备上崩溃,因为您在检查它是否存在之前正在调用[self.locationManager requestWhenInUseAuthorization];