MapKit學習筆記

1、概述
插入MapView,設置Delegate(一般爲Controller),Annotations記錄興趣位置點(AnnotationView用來顯示興趣位置點),annotation是可選的,選中的annotation會顯示callout,用來顯示信息。
2、設置地圖顯示類型:
mapView.mapType = MKMapTypeStandard;
mapView.mapType = MKMapTypeSatellite;
mapView.mapType = MKMapTypeHybrid; 
3、顯示用戶位置
設置爲可以顯示用戶位置:
mapView.showsUserLocation = YES; 
判斷用戶當前位置是否可見(只讀屬性):
userLocationVisible 
得到用戶位置座標:當userLocationVisible爲YES時
CLLocationCoordinate2D coords = mapView.userLocation.location.coordinate; 
4、座標範圍
MKCoordinateRegion用來設置座標顯示範圍。
包括兩部分:Center(CLLocationCoordinate2D struct,包括latitude和longitude),座標中心
和Span(MKCoordinateSpan struct,包括latitudeDelta和longitudeDelta),縮放級別
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(center,2000, 2000); 
以上代碼創建一個以center爲中心,上下各1000米,左右各1000米得區域,但其是一個矩形,不符合MapView的橫縱比例
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion]; 
以上代碼創建出來一個符合MapView橫縱比例的區域
[mapView setRegion:adjustedRegion animated:YES]; 
以上代碼爲:最終顯示該區域
5、Delegate
使用MapView須符合MKMapViewDelegate協議
5.1、地圖加載Delegate
當需要從Google服務器取得新地圖時
mapViewWillStartLoadingMap: 
當成功地取得地圖後
mapViewDidFinishLoadingMap: 
當取得地圖失敗後(建議至少要實現此方法)
mapViewDidFailLoadingMap:withError:
5.2、範圍變化Delegate
當手勢開始(拖拽,放大,縮小,雙擊)
mapView:regionWillChangeAnimated: 
當手勢結束(拖拽,放大,縮小,雙擊)
mapView:regionDidChangeAnimated: 
判斷座標是否在MapView顯示範圍內:
CLLocationDegrees leftDegrees = mapView.region.center.longitude –(mapView.region.span.longitudeDelta / 2.0);
CLLocationDegrees rightDegrees = mapView.region.center.longitude +(mapView.region.span.longitudeDelta / 2.0);
CLLocationDegrees bottomDegrees = mapView.region.center.latitude –(mapView.region.span.latitudeDelta / 2.0);
CLLocationDegrees topDegrees = self.region.center.latitude +(mapView.region.span.latitudeDelta / 2.0);
if (leftDegrees > rightDegrees) { // Int'l Date Line in View
leftDegrees = -180.0 - leftDegrees;
if (coords.longitude > 0) // coords to West of Date Line
coords.longitude = -180.0 - coords.longitude;
}
If (leftDegrees <= coords.longitude && coords.longitude <= rightDegrees && bottomDegrees <= coords.latitude && coords.latitude <= topDegrees) {
// 座標在範圍內
}
6、Annotation
Annotation包含兩部分:Annotation Object和Annotation View
Annotation Object必須符合協議MKAnnotation,包括兩個方法:title和subtitle,分別用於顯示註釋的標題和子標題。還有coordinate屬性,返回CLLocationCoordinate2D,表示Annotation的位置
然後,需使用mapView:viewForAnnotation: 方法來返回MKAnnotationView或者MKAnnotationView的子類用來顯示Annotation(注意:這裏顯示的不是選中Annotation後的彈出框) 
你可以子類化MKAnnotationView,然後再drawRect:方法裏面進行自己的繪製動作(這個方法很蠢)
你完全可以實例化一個MKAnnotationView,然後更改它的image屬性,這樣很簡單。
7、添加移除Annotation
添加一個Annotation
[mapView addAnnotation:annotation]; 
添加一個Annotation數組
[mapView addAnnotations:[NSArray arrayWithObjects:annotation1, annotation2, nil]]; 
移除一個Annotation
removeAnnotation: 
移除一個Annotation數組
removeAnnotations: 
移除所有Annotation
[mapView removeAnnotations:mapView.annotations];
8、選中Annotation
一次只能有一個Annotation被選中,選中後會出現CallOut(浮動框)
簡單的CallOut顯示Title和SubTitle,但你也可以自定義一個UIView作爲CallOut(與自定義的TableViewCell一樣)
可通過代碼選中Annotation:
selectAnnotation:animated: 
或者取消選擇:
deselectAnnotation:animated:
9、顯示Annotation
通過mapView:viewForAnnotation: 方法顯示Annotation,每在MapView中加入一個Annotation,就會調用此方法
示例(與tableView:cellForRowAtIndexPath: 很相似)
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
static NSString *placemarkIdentifier = @"my annotation identifier";
if ([annotation isKindOfClass:[MyAnnotation class]]) {
MKAnnotationView *annotationView = [theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
annotationView.image = [UIImage imageNamed:@"blood_orange.png"];
}
else
annotationView.annotation = annotation;
return annotationView;
}
return nil;
}
10、取得真實地址
示例:
初始化MKReverseGeocoder
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinates];
geocoder.delegate = self;
[geocoder start]; 
如果無法處理座標,則調用reverseGeocoder:didFailWithError: 方法
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
NSLog(@"Error resolving coordinates: %@", [error localizedDescription]);
geocoder.delegate = nil;
[geocoder autorelease];

如果成功,則調用reverseGeocoder:didFindPlacemark: 並把信息存儲在MKPlacemark 中
didFindPlacemark:(MKPlacemark *)placemark {
NSString *streetAddress = placemark.thoroughfare;
NSString *city = placemark.locality;
NSString *state = placemark.administrativeArea;
NSString *zip = placemark.postalCode;
// Do something with information
geocoder.delegate = nil;
[geocoder autorelease];
}

發佈了0 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章