ios - iPhone GPS导航不会退出

标签 ios iphone gps xcode5

我有一个 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/

相关文章:

android - 新的 Android Geofence Api - 示例代码在位置时不发出警报/通知

android - 如何从广播接收器中注销监听器并停止服务

java - 什么相当于hadoop 2.7.1中的hadoop-core-xxx.jar

ios - 使用原生 iOS View 提交 Google 表单数据

iphone - NSDateComponents包装在结构中?

iphone - 如何在 iOS 上重新呈现 modalViewController

iOS CoreBluetooth 更新值 : and didUpdateValueForCharacteristic:

ios - 查看附近所有可发现的蓝牙设备 - Swift iOS XCode

iphone - UIView不显示 socket

iphone - iPhone 上的 Python