2012-09-03 55 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