有谁知道这段代码的作用?

时间:2012-09-03 22:19:35

标签: iphone objective-c ios xcode ipad

CLLocation *userLoc = mapView.userLocation.location;
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

    NSLog(@"user latitude = %f",userCoordinate.latitude);
    NSLog(@"user longitude = %f",userCoordinate.longitude);

    mapView.delegate=self;

上述代码的作用是什么?不需要逐行解释,因为我理解这个术语..只是不确定这个代码用于什么..

1 个答案:

答案 0 :(得分:1)

首先,您应该学习如何挖掘苹果文档以回答这些类型的问题。我通常首先搜索XXX类引用或XXX开发人员指南。

mapview是一个MKMapView对象。见这里:http://developer.apple.com/library/ios/#DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html

userLocation返回用户的当前位置。从那些文档:

userLocation
The annotation object representing the user’s current location. (read-only)

@property(nonatomic, readonly) MKUserLocation *userLocation

然后代码获取用户位置的坐标并记录纬度和经度。

最后,将委托设置为self意味着该类将实现MKMapViewDelegate协议的回调。从那些文档:

delegate
The receiver’s delegate.

@property(nonatomic, assign) id<MKMapViewDelegate> delegate
Discussion
A map view sends messages to its delegate regarding the loading of map data and changes in     the portion of the map being displayed. The delegate also manages the annotation views used to highlight points of interest on the map.

The delegate should implement the methods of the MKMapViewDelegate protocol.

请参阅此处了解代表的内容:What exactly does delegate do in xcode ios project?

因此,回调允许您将代码插入到地图视图的管道执行中,并提供像viewForAnnotation这样的数据。

以下是MKMapVeiwDelegate上的文档(mapview的回调):

http://developer.apple.com/library/ios/#DOCUMENTATION/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html#//apple_ref/occ/intf/MKMapViewDelegate