核心位置返回00

时间:2011-05-13 02:28:32

标签: iphone delegates core-location cllocationmanager cllocation

这是完整的方法,我最初没有放下半部分,因为我知道它有效:

 -(void)showDirections
{
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [locationManager setDelegate:self];

    CLLocation *location = [locationManager location];

    CLLocationCoordinate2D coordinate = [location coordinate];
    NSNumber *myLatitude = [NSNumber numberWithDouble:coordinate.latitude];
    NSNumber *myLongitude = [NSNumber numberWithDouble:coordinate.longitude];
    double myLatitude2 = [myLatitude doubleValue];
    double myLongitude2 = [myLongitude doubleValue];


    NSArray *array = [dataHold objectForKey:@"Subtree"];
    NSString *latitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:4]];
    NSString *longitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:5]];
    double clubLatitude = [latitude doubleValue];
    double clubLongitude = [longitude doubleValue];
    CLLocationCoordinate2D start = { myLatitude2, myLongitude2};    
    CLLocationCoordinate2D destination = { clubLatitude, clubLongitude};        
    NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",start.latitude, start.longitude, destination.latitude, destination.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];

}

2 个答案:

答案 0 :(得分:1)

两个问题:

1)您正在进行NSNumber框/ unbox循环,无论如何都无用

2)核心位置是异步的,因此您无法立即返回用户的位置,必须使用委托方法检索位置是否/何时可用。

以下是 应如何工作的示例:

 -(void)showDirections
{
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [locationManager setDelegate:self];

    [locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    CLLocationCoordinate2D coord = [newLocation coordinate];
    NSArray *array = [dataHold objectForKey:@"Subtree"];
    NSString *latitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:4]];
    NSString *longitude = [NSString stringWithFormat:@"%@",[array objectAtIndex:5]];
    double clubLatitude = [latitude doubleValue];
    double clubLongitude = [longitude doubleValue];
    NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", coord.latitude, coord.longitude, clubLatitude, clubLongitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
}

答案 1 :(得分:1)

代码的第四行是问题的开始。您无法立即获取该位置,您需要等到位置服务通知代表该位置可用。我将在您的代码中添加一些注释来解释:

-(void)showDirections
{
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [locationManager setDelegate:self];

    // this is not where you should be looking 
    CLLocation *location = [locationManager location];

    // the rest of this function is not included in this example ...
}

由于您将委托设置为“self”,因此当前类实现了“CLLocationManagerDelegate”协议,该协议定义了以下两个函数:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { /* */ }
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { /* */ }

这是您将获得提供给您的位置的地方。

别忘了像这样更新你的课程定义:

@interface MyClass : NSObject <CLLocationManagerDelegate> {

作为旁注,似乎你并不完全理解代表和他们所服务的目的,如果你是Objective-C的新手,这很好,但是对他们更熟悉肯定会让你的生活变得有点更容易。

其他一些有用的东西: