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

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

    mapView.delegate=self;

上面的代码到底做了什么?无需逐行解释,因为我了解行话..只是不确定此代码的用途..

4

1 回答 1

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.

请参阅此处了解委托是 什么:委托在 xcode ios 项目中到底做了什么?

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

这是 MKMapVeiwDelegate 上的文档(地图视图的回调):

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

于 2012-09-03T22:28:50.267 回答