2012-09-03 52 views
-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; 

上面的代码是做什么的?不需要一行一行的解释,因为我的理解行话..只是不确定这个代码是用于什么..有谁知道这段代码做了什么?

回答

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