我尝试使用 Core Location 编写坐标,它运行良好,唯一的问题是,在坐标方面它是一种蔬菜。
这是获取坐标的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (startingLocation == nil)
self.startingLocation = newLocation;
NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.latitude];
latitudeLabel.text = latitudeString;
NSString *longitudeString = [[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.longitude];
longitudeLabel.text = longitudeString;
NSString *horizontalAccuracyString = [[NSString alloc] initWithFormat:@"%g",newLocation.horizontalAccuracy];
horizontalAccuraryLabel.text = horizontalAccuracyString;
NSString *altitudeString = [[NSString alloc] initWithFormat:@"%g",newLocation.altitude];
altitudeLabel.text = altitudeString;
NSString *verticalAccuracyString = [[NSString alloc] initWithFormat:@"%g",newLocation.verticalAccuracy];
verticalAccuracyLabel.text = verticalAccuracyString;
CLLocationDistance distance = [newLocation getDistanceFrom:startingLocation];
NSString *distanceString = [[NSString alloc] initWithFormat:@"%g",distance];
distanceTraveledLabel.text = distanceString;
}
它给了我 Lat: 37.7858 Long: -122.406 这是在美国的某个地方,而我正在运行来自欧洲、罗马尼亚的代码......
可能是因为它是从模拟器运行的吗?
我也知道 locationManager:didUpdateToLocation:fromLocation 已弃用,但我尝试使用:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
但我不知道如何访问 [location lastObject] 的元素。