【iOS中如何获得合法的CLLocationCoordinate2D类型的坐标值】
之前折腾给Your Second iOS App:BirdWatching添加地图的支持的过程中,最终发现,如果坐标值赋值的话,一定要注意,CLLocationCoordinate2D类型的坐标值,即经度和纬度的组合,是合法的值,才可以。
所以,此处专门总结一下,iOS中,如何得到有效的,合法的CLLocationCoordinate2D类型的值:
目前所知,至少有以下几种来源:
1. 从地图类网站获得的
比如从:
http://www.gpsspg.com/maps.htm
随便找到的某个坐标,然后其中显示出的“谷歌纬度经度”,才是正确的值。
比如:
谷歌纬度经度:31.27006030476515,120.70549774169922
对应的代码为:
CLLocationCoordinate2D customLoc2D = CLLocationCoordinate2DMake(31.27006030476515, 120.70549774169922);
2。从别的已有的某个某种类型的坐标变量转换而得。
比如此处的CLLocation类型的变量。
关于如何将CLLocation转换为CLLocationCoordinate2D参考后面的总结。
而如果是其他类型的坐标变量,则可以参考最开始所摘录的官网的资料:
Location Awareness Programming Guide – Displaying Maps
中所总结的所涉及的类:
Convert from | Convert to | Conversion routines |
Map coordinates | Points | convertCoordinate:toPointToView: (MKMapView) convertRegion:toRectToView: (MKMapView) |
Map coordinates | Map points | |
Map points | Map coordinates | |
Map points | Points | pointForMapPoint: (MKOverlayView) rectForMapRect: (MKOverlayView) |
Points | Map coordinates | convertPoint:toCoordinateFromView: (MKMapView) convertRect:toRegionFromView: (MKMapView) |
Points | Map points | mapPointForPoint: (MKOverlayView) mapRectForRect: (MKOverlayView) |
【把CLLocation转换为CLLocationCoordinate2D】
其实很简单,由于本身CLLocation就有个属性叫做coordinate:
coordinate
The geographical coordinate information. (read-only)
@property(readonly, NS_NONATOMIC_IPHONEONLY) CLLocationCoordinate2D coordinate
Discussion
When running in the simulator, Core Location assigns a fixed set of coordinate values to this property. You must run your application on an iOS-based device to get real location values.
Special Considerations
In iOS, this property is declared as
nonatomic
. In OS X, it is declared asatomic
.Availability
- Available in OS X v10.6 and later.
Declared In
CLLocation.h
所以,直接通过:
cllocationValue.coordinate
即可得到对应的CLLocationCoordinate2D类型的值了。
对应示例代码:
#pragma mark CLLocationManagerDelegate -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ [self.locationMapView setCenterCoordinate:newLocation.coordinate animated:YES]; }
转载请注明:在路上 » iOS中如何获得合法的CLLocationCoordinate2D类型的坐标值 + 把CLLocation转换为CLLocationCoordinate2D