0

我正在使用 cllocation 管理器在我的应用程序委托类中获取用户当前位置。类似这样的事情:

-(void) getUserCurrentLocation
{
    destinationForProgressView = .25;
    if(![CLLocationManager locationServicesEnabled])
    {
        UIAlertView *enableGpsInYourDevice = [[UIAlertView alloc] initWithTitle:@"" message:@"Go to settings and enable location services" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [enableGpsInYourDevice show];
    }

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    // This is the most important property to set for the manager. It ultimately determines how the manager will
    // attempt to acquire location and thus, the amount of power that will be consumed.
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // When "tracking" the user, the distance filter can be used to control the frequency with which location measurements
    // are delivered by the manager. If the change in distance is less than the filter, a location will not be delivered.
    locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters;
    // Once configured, the location manager must be "started".
    [locationManager startUpdatingLocation];
}

我符合位置协议并实现其委托方法:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = [locations lastObject];

    latitude =  newLocation.coordinate.latitude;

    longitude = newLocation.coordinate.longitude;

    NSLog(@"lattitude from appDelegate   is %.8f", [[locations lastObject] coordinate].latitude);

    NSLog(@"longitude from appDelegate is %.8f", [[locations lastObject] coordinate].longitude);
}

纬度和经度在 .h 中的位置:@property (nonatomic, assign) CLLocationDegrees latitude; @property (nonatomic, assign) CLLocationDegrees 经度;

通过这种方式,我可以获得当前位置,但如果我从模拟器调试器快速更改纬度和经度,它不会得到更新。

然后在我的视图控制器中,我可以简单地访问纬度和经度属性,如下所示:

-(void) getLocationCoordinates
{
    self.destinationForProgressView = .25;

    //[DELEGATE getUserCurrentLocation];**NOTE**

    latitude = [DELEGATE latitude];
    longitude = [DELEGATE longitude];

    if (latitude && longitude) {
        NSString *prepareMessagebody = nil;
        memberID = [USERDATASINGLETON getMemberID];
        prepareMessagebody = [NSString stringWithFormat:@"Latitude is %.8f\n Longitude is %.8f\n Member-ID is %@",latitude, longitude, memberID];
        NSLog(@"test %@", prepareMessagebody);

        NSArray *reciepients = [NSArray arrayWithObjects:@"stack@over.com", nil];

        NSArray *passingobject = [NSArray arrayWithObjects:reciepients, prepareMessagebody, nil];

        [self performSelector:@selector(showMailPickerwithObject:)
                   withObject:passingobject
                   afterDelay:1];
    }
    else{
        [self showalertIfLocationServicesDisabled];
    }
}

当我再次调用委托方法时,请参阅注释和注释代码 //[DELEGATE getUserCurrentLocation]; 我能够获得经常更新的位置。 我是否应该再次调用委托类来更新位置,而不是我的委托更新位置 (@property (nonatomic, assign) CLLocationDegrees latitude and @property (nonatomic, assign) CLLocationDegrees longitude;)并因此进入我的视图控制器。请建议?

4

1 回答 1

0

这似乎是此处概述的模拟器问题。你能在实际设备上测试它吗?

于 2013-05-24T16:48:58.453 回答