0

我根据 iOS8.1 SDK 对我的应用程序进行了必要的更改以跟踪位置。我正在使用下面的代码来获取纬度和经度。即使我没有得到纬度和经度值。我是否需要做除此之外的任何更改以获得纬度和经度值?

在 appdelegate.h 中:

CLLocationManager *locationManager;

在 appdelegate.m 中:

- (void)applicationDidBecomeActive:(UIApplication *)application{
    if (locationManager==nil) {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [locationManager requestAlwaysAuthorization];
        }
        [locationManager startUpdatingLocation];
    }

    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];
    NSLog(@" location details %f %f ",locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);
}

在 Info.plist 文件中

 <key>NSLocationAlwaysUsageDescription</key>
<string>This will be used to obtain or track your location.</string>
4

3 回答 3

2

我相信您没有使用 CLLocationManagerDelegate 在您的 CLLocationManager 实际检索到位置时得到通知。尝试立即从您的位置管理器获取位置几乎肯定每次都不会返回任何值,因为您只是请求它开始更新。

您需要执行以下操作:

1) 在 appdelegate.m 中将您的 appdelegate 类声明为您的 CLLocationManager 的委托。

self.locationManager = self;

2) 告诉您的 appdelegate.h 文件采用 CLLocationManagerDelegate 协议

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>

3)在appdelegate.m中实现CLLocationManagerDelegate提供的locationManager:didUpdateLocations方法

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

4)在您刚刚实施的方法中获取您正在寻找的位置。:)

我建议您查看 Apple 提供的文档,因为您使用的是 C 风格的解引用运算符,我怀疑您是 Objective-C 语言的新手

于 2014-11-05T12:57:54.900 回答
0

您是否检查了设置中的权限?您应该期望在委托方法中将该位置传递给您。用户接受后。否则我不确定经理会在现场找到位置。

于 2014-11-05T12:58:29.517 回答
0

您还需要启用服务

if([CLLocationManager resolveClassMethod:@selector(locationServicesEnabled)]) {
    [CLLocationManager locationServicesEnabled];
}
于 2014-11-05T13:03:28.090 回答