gpt4 book ai didi

ios - 在我的 iPhone 中显示当前位置

转载 作者:行者123 更新时间:2023-11-28 22:29:28 26 4
gpt4 key购买 nike

我正在实现从我当前位置到某个目的地位置的路线指示。

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

我发现问题是第一次,当前位置显示为 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

最佳答案

问题在于您正在制作位置管理器,然后期望它在几微秒后拥有位置。您应该设置位置管理器的委托(delegate),然后等待它被调用。当 LM 有一个位置时,它会调用 didUpdateLocation,你可以用它来调用 updateCurrentLabel

关于ios - 在我的 iPhone 中显示当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17875488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com