【问题标题】:How to get GPS coordinate debugging with simulator in iOS?如何在 iOS 中使用模拟器进行 GPS 坐标调试?
【发布时间】:2026-01-03 07:45:01
【问题描述】:

我尝试获取我所在位置的坐标。

我*ed所以我编码如下:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];

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

self.longitude=coordinate.longitude;
self.latitude=coordinate.latitude;

NSLog(@"dLongitude : %f",self.longitude);
NSLog(@"dLatitude : %f", self.latitude);

但我总是得到 0。上面的代码错了吗?或者 我的模拟器没有设置 GPS 定位?

我不明白为什么我无法获得坐标。

【问题讨论】:

    标签: ios objective-c gps


    【解决方案1】:

    首先,您的代码中有几个问题:

    1. 您应该将 locationManager 保留在某处以保持其运行
    2. 实现-locationManager:didUpdateLocations:-locationManager:didFailWithError: 委托方法
    3. 另外,如果您使用的是 iOS 8,则应添加 [locationMamager requestWhenInUseAuthorization];[locationMamager requestAlwaysAuthorization];
    4. 在 Info.plist 中相应地指定 NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription

    你可以用Xcode模拟定位,看Apple的资料:https://developer.apple.com/library/ios/recipes/xcode_help-debugger/articles/simulating_locations.html

    【讨论】:

    • 按照 Azat 告诉您的操作后,您可以在应用运行时通过选择 Debug->Location->CustomLocation... 来模拟您的当前位置。
    • LoVo ,你的意思是,调试 -> 模拟位置 -> 选择地标?
    • Azat,“1 号”是什么意思?我确实在 controller.h 接口文件中声明了 'CLLocationManager *locationManager' 和 。是你的意思吗?
    • @LKM 如果locationManager 是 ivar 或某个类的属性,则可以。如果它只是自动变量,那么位置管理器将在该方法结束后释放
    • @Azat 我想我做了你给我的一切,但没有调用方法 didUpdateLocations。而方法 didFailWithError,error.description 说 'Error Domain=kCLErrorDomain Code=0 "The operation could not be completed. (kCLErrorDomain error 0.)"' 你能配置出来吗?谢谢你给我信息。我学了很多
    【解决方案2】:

    [CLLocationManager location] 将返回您最近检索到的用户位置,但正如文档所述:

    如果从未检索到任何位置数据,则此属性的值为 nil。

    一开始你的位置仍然未知。当 CLLocationManager 找到您的位置时,您应该使用委托方法做出反应。实现这个方法:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
        CLLocation *location = [locations lastObject];
        //User your location...
    }
    

    看看documentation

    【讨论】:

    • 谢谢你的诚意。然而,Raspu 没有调用方法 didUpdateLocations。我在 info.plist 中添加键 NSLocationWhenInUseUsage
    【解决方案3】:
    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
        BOOL shouldIAllow = FALSE;
        NSString* locationStatus = @"";
    
        switch (status) {
            case kCLAuthorizationStatusRestricted:
                locationStatus = @"Restricted Access to location";
                break;
            case kCLAuthorizationStatusDenied:
                locationStatus = @"User denied access to location";
                break;
            case kCLAuthorizationStatusNotDetermined:
                locationStatus = @"Status not determined";
            default:
                locationStatus = @"Allowed to location Access";
                shouldIAllow = TRUE;
                break;
        }
    
        if (shouldIAllow) {
            NSLog(@"Location to Allowed");
            // Start location services
            [_locationManager startUpdatingLocation];
        } else {
            NSLog(@"Denied access: \(locationStatus)");
        }
    }
    

    【讨论】:

      最近更新 更多