日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Leonbao:MapKit学习笔记

發布時間:2025/4/16 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leonbao:MapKit学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以下僅作了解, 實際使用以百度地圖居多, 因為百度地圖有動態路徑規劃等接口

?

MapKit學習筆記 ??

原帖: http://www.cocoachina.com/bbs/read.php?tid-66687-fpage-6.html

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];
}

轉載于:https://www.cnblogs.com/apem/p/4630080.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Leonbao:MapKit学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。