didUpdateLocation方法从不调用 [英] didUpdateLocation Method Never Called

查看:599
本文介绍了didUpdateLocation方法从不调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iphone sdk4.0上创建一个应用程序。在那里确实没有调用更新位置方法。
我在下面给出了我的代码。请帮助。提前谢谢。

I am making one app on iphone sdk4.0.In that did update location method never called. I have given my code below.Please help.Thanks in advance.

-(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];
}


推荐答案

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

your codes seems ok. now these can be possible reasons :

你的init:尝试检查是否有locationServicesEnabled。

on your init: try to check if there is locationServicesEnabled or not.

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

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

other reason, you may disallow to get location to your app. solution: simply remove your application from iPhone and re-build, now it should popup dialog to Allow location.

使用此方法检查错误

- (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,则为
,则可能会出现此问题。

otherwise all seems ok, you were already running ios 4.0 , which can be install in iPhone 3G and later, if it was iPhone 2g, then this problem may occur.

这篇关于didUpdateLocation方法从不调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆