如何在iOS中使用模拟器进行GPS坐标调试?

LKM*_*LKM 4 gps objective-c ios

我试着获得我所在位置的坐标.

我stackoverflowed所以我编码如下:

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);
Run Code Online (Sandbox Code Playgroud)

但我总是一直都是0.上面的代码是错的吗?或者我没有为GPS定位设置我的模拟器?

我不明白为什么我在协调方面遇到麻烦.

Aza*_*zat 6

首先,您的代码中存在以下几个问题:

  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