0

現在の場所から目的地へのルート案内を実装しています。

特定のページに入ると、アプリケーションがクラッシュします。しかし、アプリをもう一度実行すると、ルート案内が表示されます。

問題は、初めて現在の場所が0.00000,0.00000座標として表示されていることです。アプリを 2 回目に起動すると、現在地が正しく表示されます。

何がうまくいかないのですか?初めて自分の現在位置を正確に取得したい。

以下にコードを書いています。一度それをチェックして、私のコードのどこに問題があるか教えてください

#import "MapWithRoutesViewController.h"
#import "TFSViewController.h"

@implementation MapWithRoutesViewController

@synthesize locationManager,lat,lon;

- (void) updateCurrentLabel {
    NSObject *latitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];
    NSObject *longitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];

    locationlable.text = [NSString stringWithFormat: @"Current Location: %@,%@", latitude, longitude];
}

- (void)viewDidLoad {
    [self getCurrentLocation];
    [super viewDidLoad]; 
}

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [self updateCurrentLabel]; 
}

-(void) getCurrentLocation {
    MapView* mapView = [[[MapView alloc] initWithFrame:
                         CGRectMake(0, 90, self.view.frame.size.width, self.view.frame.size.height)] autorelease];      [self.view
addSubview:mapView];
    Place* home = [[[Place alloc] init] autorelease];


    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;

    if (locationManager==NO) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled"
                                                        message:@"To re-enable, please go to Settings and turn on Location Service for
this app."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    } else {
        //        if (locationManager==NO)
        //            [mapView.lo addObserver:self forKeyPath:@"location"
        // options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
        // context:nil];
    }

    locationManager.pausesLocationUpdatesAutomatically = NO;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;

    [locationManager setPausesLocationUpdatesAutomatically:YES];
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];


    CLLocation *Loc = [locationManager location];
    CLLocationCoordinate2D coordinate = [Loc coordinate];

    NSObject *latitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];
    NSObject *longitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];


    NSLog(@"lat,lon %@%@",latitude,longitude);

    home.name = @"";
home.description = @"";
home.latitude = coordinate.latitude;
home.longitude = coordinate.longitude; 

    Place* office = [[[Place alloc] init] autorelease];
office.name = @"";
office.description = @"";
office.latitude = 22.0000;
    office.longitude = 88.0008;

    [mapView showRouteFrom:home to:office];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated. 
}

-(IBAction)back:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(IBAction)tfs:(id)sender{
    TFSViewController *tfs = [[TFSViewController alloc]initWithNibName:@"TFSViewController" bundle:nil];

    [self presentViewController:tfs animated:YES completion:nil];
}

@end
4

2 に答える 2