【问题标题】:didUpdateLocation Method Never Called从未调用过 didUpdateLocations 方法
【发布时间】:2011-07-19 10:55:09
【问题描述】:

我正在 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];
}

【问题讨论】:

  • 终于成功了……我什么都没做。我只是做了一个新项目并复制了其中的所有类。我不知道如何?但它成功了……

标签: iphone ios ios4 cllocationmanager cllocation


【解决方案1】:

此外,在 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/

【讨论】:

  • ...一般不鼓励使用 Always 方法,如果您不确定自己在做什么,请使用其他方法。
【解决方案2】:

确保您已将 CLLocationManager 添加为属性。

@property (nonatomic , strong) CLLocationManager *locationManager;

【讨论】:

  • Gabriel 你向我展示了一个好方法。我现在得到了解决方案。我尝试调用 didupdateLocation 方法,但即使我应用了除你的建议之外的所有内容,它也没有调用。最后我应用 CLLocationManager 作为属性。现在调用委托方法,我得到更新位置(纬度,经度)值。
  • 能否请您告诉我关于属性 CLLocationManager 的原因或解释以及为什么没有属性不起作用?
  • 因为如果它是一个属性,它有一个强引用。如果不是属性,它会在方法之外被释放。所以它不能接收来自委托的回调。 @user3182143
【解决方案3】:

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

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

【讨论】:

  • 不..我没有在模拟器上检查..我在 iphone 设备上检查它
  • 您是否尝试过手动调用当前位置并确保您的位置有效?您可以使用 locationManager.location 来做到这一点。
  • 这不是真的,你现在可以模拟运动了。 Simulator -> Debug -> Location
【解决方案4】:

在您 alloc 和 init 您的位置经理之后,检查是否允许位置服务。此代码来自 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 基站。

【讨论】:

    【解决方案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 可能会出现这个问题。

    【讨论】:

      【解决方案6】:

      您的委托中的-locationManager:didFailWithError: 是否曾经被调用过?也许您在某个时候拒绝访问位置数据,现在没有收到提示,但访问被拒绝。

      【讨论】:

        猜你喜欢
        • 2013-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-24
        • 2017-09-15
        • 1970-01-01
        相关资源
        最近更新 更多