7

我正在 iphone sdk4.0 上制作一个应用程序。在那个更新位置方法从未调用过。我在下面给出了我的代码。请帮助。提前致谢。

-(id)init
{
    [super init];

    obj=[[UIApplication sharedApplication]delegate];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    //locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];

    return self;

}

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

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

    obj=[[UIApplication sharedApplication]delegate];

    location=[[CLLocation alloc]initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
    obj.lattitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];

    obj.longitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
    //location=[[CLLocation alloc]initWithLatitude:39.9883 longitude:-75.262227];
}
4

6 回答 6

18

此外,在 iOS8 中你必须有两个额外的东西:

  • 将密钥添加到您的 Info.plist 并请求位置管理器的授权以启动它。

    • NSLocationWhenInUseUsageDescription

    • NSLocationAlwaysUsageDescription

  • 您需要为相应的定位方法请求授权。

    • [self.locationManager requestWhenInUseAuthorization]

    • [self.locationManager requestAlwaysAuthorization]

代码示例:

self.locationManager = [[CLLocationManager alloc] init];
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];

资料来源: http: //nevan.net/2014/09/core-location-manager-changes-in-ios-8/

于 2014-09-22T16:32:28.310 回答
5

嗨,你的代码似乎没问题。现在这些可能是可能的原因:

在您的初始化上:尝试检查是否有 locationServicesEnabled。

locationManager = [[CLLocationManager alloc] init];
if(locationManager.locationServicesEnabled == NO){
    //Your location service is not enabled, Settings>Location Services  
}

其他原因,您可能不允许获取应用程序的位置。解决方案:只需从 iPhone 中删除您的应用程序并重新构建,现在它应该弹出对话框以允许位置。

用它来检查错误

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{       
    NSLog(@"Error while getting core location : %@",[error localizedFailureReason]);
    if ([error code] == kCLErrorDenied) {
        //you had denied 
    }
    [manager stopUpdatingLocation];
}

否则一切正常,您已经在运行 ios 4.0 ,可以安装在 iPhone 3G 及更高版本中,如果是 iPhone 2g,则可能会出现此问题。

于 2011-03-21T06:17:27.993 回答
4

确保将 CLLocationManager 添加为属性。

@property (nonatomic, strong) CLLocationManager *locationManager;

于 2014-01-28T17:39:32.093 回答
3

如果您在模拟器上对此进行测试,它将无法正常工作。

发生这种情况是因为该方法仅在设备更改位置时调用(并且模拟器永远不会更改位置)。

于 2011-03-21T07:09:09.357 回答
1

-locationManager:didFailWithError:的代表有没有被叫过?也许您在某个时候拒绝访问位置数据,现在没有收到提示,但访问被拒绝。

于 2011-03-21T06:09:21.237 回答
0

在您allocinit您的位置经理之后,检查是否允许位置服务。此代码来自 Apple 的“LocateMe”代码示例

if (locationManager.locationServicesEnabled == NO) {
    UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [servicesDisabledAlert show];
    [servicesDisabledAlert release];
}

如果您使用模拟器进行测试,请尝试使用设备进行测试。该模拟器要求计算机的 WiFi 已打开,并且它可以定位其已知位置数据库中的 WiFi 基站。

于 2011-03-21T06:35:57.160 回答