0

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

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

}
4

2 回答 2

1

两个问题:

1)你正在做一个NSNumber没有任何用处的装箱/拆箱循环

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]];
}
于 2011-05-13T02:36:53.567 回答
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 的新手,这很好,但更熟悉它们肯定会让您的生活更轻松一些。

其他一些有用的东西:

于 2011-05-13T02:57:59.793 回答