Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在我的iphone中显示当前位置_Ios_Objective C_Mkmapview_Xcode4.5_Routes - Fatal编程技术网

Ios 在我的iphone中显示当前位置

Ios 在我的iphone中显示当前位置,ios,objective-c,mkmapview,xcode4.5,routes,Ios,Objective C,Mkmapview,Xcode4.5,Routes,我正在执行从当前位置到某个目的地位置的路线指示 当我进入特定页面时,我的应用程序崩溃。但如果我再次运行该应用程序,它会显示路线方向 我发现问题是第一次,当前位置显示为0.00000,0.00000坐标。第二次启动应用程序时,当前位置显示正确 出什么事了? 我想在第一时间正确地得到我当前的职位 我正在写下面的代码。一旦请检查出来,让我知道我的代码中的问题在哪里 #import "MapWithRoutesViewController.h" #import "TFSViewController.h"

我正在执行从当前位置到某个目的地位置的路线指示

当我进入特定页面时,我的应用程序崩溃。但如果我再次运行该应用程序,它会显示路线方向

我发现问题是第一次,当前位置显示为
0.00000,0.00000
坐标。第二次启动应用程序时,当前位置显示正确

出什么事了? 我想在第一时间正确地得到我当前的职位

我正在写下面的代码。一旦请检查出来,让我知道我的代码中的问题在哪里

#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

问题是,您正在创建位置管理器,然后期望它在几微秒后拥有一个位置。您应该设置位置管理器的委托,然后等待调用它。当LM有一个位置时,它将调用didUpdateLocation,您可以使用它调用updateCurrentLabel,您应该使用委托方法-locationManager:didUpdateLocations:来获取位置。CLLocationManager需要几秒钟来建立GPS连接并确定当前位置

:

还可以使用CLLocation的HorizontalAccurance属性检查位置的准确性,以确定是要使用该值还是放弃该值


您是否要签入模拟器?在模拟器上显示一些代码。在何处实施位置更新并指定地图属性?特别是在哪个视图控制器方法中,您将这些代码实现为“viewDidload”、“viewWillAppeare”?请回答这些问题。因为有太多可能出错。显示你的代码……请显示你的代码!我已经发布了一次我的代码,请在这里查看