我有一个 iPhone 应用程序,其中一个 View 上有一个导航按钮。当用户单击此按钮时,它会使用手机 CLLocationManager 打开 iPhone 的 GPS 应用程序。然后,它获取用户当前位置并显示到预定义设置点的导航。
这里一切正常,但是当我退出 GPS 导航(通过单击手机主页按钮)并返回应用程序时,应用程序会显示带有导航按钮的 View 一两秒,然后无需点击任何按钮即可再次启动 GPS 导航。我也无法单击此处的任何按钮,例如顶部导航栏中的后退按钮。
谁能告诉我为什么会发生这种情况以及如何阻止它?这是我的 GPS 导航代码。
在我的 viewDidLoad 中,我以编程方式创建了导航按钮
UIButton *mapButton = [[UIButton alloc] init];
mapButton = [UIButton buttonWithType:UIButtonTypeCustom];
mapButton.tag = 1;
mapButton.frame = CGRectMake(20, 430, 280.f, 40.f);
UIImage *mapButtonImage = [UIImage imageNamed:@"gettingHereMapButton.png"];
[mapButton setBackgroundImage:mapButtonImage forState:UIControlStateNormal];
[self.view addSubview:mapButton];
[mapButton addTarget:self action:@selector(mapButtonClicked) forControlEvents:UIControlEventTouchUpInside];
然后进入
- (void) mapButtonClicked
{
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@"Alert"
message:@"An error has occured. Please try again later."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK",
nil];
[alert show];
}
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
CLLocation *oldLocation;
NSString *destAddress = @"52.269444, -9.708674";
NSString *url = [NSString stringWithFormat: @"http://maps.apple.com/maps?saddr=%f%f&daddr=%@",
newLocation.coordinate.latitude,
newLocation.coordinate.longitude,
[destAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
我需要向此 View 添加什么才能阻止这种情况发生?
最佳答案
问题是,当您执行[locationManager startUpdatingLocation];
时,locationManager会多次调用didUpdateLocations
。
要修复它,请在
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
添加这一行:
[manager stopUpdatingLocation];
就在上面:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
关于ios - iPhone GPS导航不会退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20299165/