最新消息:20181230 VPS服务器已从Linode换到腾讯云香港,主题仍用朋友推荐的大前端D8

iOS中如何获得合法的CLLocationCoordinate2D类型的坐标值 + 把CLLocation转换为CLLocationCoordinate2D

iOS crifan 1690浏览 0评论

【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

MKMapPointForCoordinate

Map points

Map coordinates

MKCoordinateForMapPoint

MKCoordinateRegionForMapRect

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 as atomic.

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

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
62 queries in 0.089 seconds, using 9.41MB memory