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

2つの問題:

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

コードの4行目は、問題が始まるところです。すぐに場所を取得することはできません。位置情報サービスが代理人に場所が利用可能であることを通知するまで待つ必要があります。説明するために、コードにコメントを追加します。

-(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 ...
}

Since you set the delegate as "self", the current class but implement the "CLLocationManagerDelegate" protocol, which defines the following two functions:

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

That is where you will get a location provided to you.

Dont forget to update your class definition like so:

@interface MyClass : NSObject <CLLocationManagerDelegate> {

And as a side note, it seems like you do not fully understand delegates and the purpose that they serve, which is fine if you are new to Objective-C, but getting more familiar with them will certainly make your life a bit easier.

Some other helpful stuff:

于 2011-05-13T02:57:59.793 に答える