获得CLLocationCoordinate2D的有效方法是什么?
在.h:
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
在.m
//Called when the location can be obtained
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//Get the latitude and longitude location
CLLocation *lastLocation = [locations lastObject];
_latitude = [[NSString alloc] initWithFormat:@"%f", lastLocation.coordinate.latitude];
_longitude = [[NSString alloc] initWithFormat:@"%f", lastLocation.coordinate.longitude];
}
是这样的:
_coordinate = lastLocation.coordinate;
或者这个:
_coordinate = manager.location.coordinate;
答案 0 :(得分:0)
首先阅读评论中已经说明的文档,因为这是位置编程必须知道的。
第二个注意不是你从委托获得的第一个数据,因为它可能是缓存数据(你需要检查时间戳)或无效数据(水平精度小于零)。
您可以使用该代码:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation * newLocation = [locations lastObject];
if (newLocation.horizontalAccuracy < 0) {
return;
}
NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];
if (abs(interval)>5) {
return;
}
//YOUR CODE HERE
}
希望这有帮助,Andrea