【iOS】Mapkit的使用:地圖顯示泼各、定位、大頭針晨另、氣泡等
(1)首先我們要像下圖這樣子打開地圖功能:
XCode會自動給我們的項(xiàng)目進(jìn)行配置笋轨,例如會自動給我們添加MapKit.frameworks。
(2)在需要使用地圖的類中
#import
(3)顯示地圖有兩種方法:
<1>使用Interface Builder, 拖一個(gè)Map view對象到View中
<2>使用純代碼秆麸,創(chuàng)建一個(gè)MKMapView的實(shí)例,使用initWithFrame:來進(jìn)行初始化及汉,最后將它作為subview添加到windows或者其它UIView上沮趣。
因?yàn)橐粋€(gè)map view是一個(gè)UIView子類奋岁,所以可以像操作其它View那樣操作map view履羞,因此可以給map view添加任何subview,但是添加的subview并不會隨著地圖內(nèi)容的移動而移動撤摸,如果需要跟隨移動温眉,必須使用annotations 或者 overlays缸匪。
新建的地圖,默認(rèn)只是顯示地圖數(shù)據(jù)以及響應(yīng)用戶手勢类溢。通過創(chuàng)建MKMapCamera實(shí)例凌蔬,可以進(jìn)行3D地圖顯示,通過設(shè)置mapType屬性可以設(shè)置地圖來顯示衛(wèi)星地圖、衛(wèi)星地圖和地圖數(shù)據(jù)的混合視圖砂心,通過改變屬性rotateEnabled, pitchEnabled, zoomEnabled,scrollEnabled 等懈词,可以限制用戶的控制權(quán)限。通過實(shí)現(xiàn)代理MKMapViewDelegate 辩诞,可以響應(yīng)用戶的手勢操作坎弯。
以下為使用第<2>種方法來顯示地圖:
[objc]view plaincopy
mapView?=?[[MKMapViewalloc]initWithFrame:self.view.bounds];
[self.viewaddSubview:mapView];
運(yùn)行效果如下:
步驟1中添加的地圖,默認(rèn)會顯示整個(gè)地球的視圖躁倒,可以通過修改它的Region屬性來設(shè)置地圖初始化時(shí)默認(rèn)的顯示區(qū)域荞怒,這個(gè)屬性是一個(gè)MKCoordinateRegion結(jié)構(gòu)體,定義如下:
[objc]view plaincopy
typedefstruct{
CLLocationCoordinate2D?center;
MKCoordinateSpan?span;
}?MKCoordinateRegion;
其中span是使用度秧秉、分褐桌、秒為單位的,一度約等于111km象迎,但是我們一般習(xí)慣使用的是長*寬的數(shù)據(jù)荧嵌,因此可以使用MKCoordinateRegionMakeWithDistance方法將常用的長和寬數(shù)據(jù)轉(zhuǎn)化為需要的以度為單位的數(shù)據(jù)。下面就是將地圖的顯示范圍設(shè)置為中心點(diǎn)為經(jīng)緯度(29.454686,106.529259)砾淌,南北方向和東西方向均為5km的區(qū)域
[objc]view plaincopy
[mapViewsetRegion:MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(29.454686,106.529259),5000,5000)animated:YES];
添加上面一句代碼后啦撮,步驟1中的視圖變成了下圖的效果:
3.顯示3D地圖
3D地圖是從某個(gè)海拔高度點(diǎn)以一定角度俯瞰2D地圖的視圖,在iOS7和OSX10.9及以后版本汪厨,可以使用MKMapCamera類來調(diào)整3D地圖赃春。
一個(gè)MKMapCamera實(shí)例類camera,使用下面的屬性來確定3D地圖:
??海拔高度(Altitude). camera的距離2D地圖平面的高度,單位米
??斜度(Pitch).camera相對于地面傾斜的角度(其中0度表示垂直往下看劫乱,所以效果是標(biāo)準(zhǔn)的2D地圖)
??方向(Heading). camera面對的方向
? 中心(Center). 顯示在屏幕正中間的地圖表面的點(diǎn)
或許舉一個(gè)例子就秒懂了织中,下面的代碼就是實(shí)現(xiàn)從經(jīng)緯度(29.545686,106.628259),高度為100的空中俯瞰經(jīng)緯度(29.454686,106.529259)的效果:
[objc]view plaincopy
//?Create?a?coordinate?structure?for?the?location.
CLLocationCoordinate2D?ground?=?CLLocationCoordinate2DMake(29.454686,106.529259);
//?Create?a?coordinate?structure?for?the?point?on?the?ground?from?which?to?view?the?location.
CLLocationCoordinate2D?eye?=?CLLocationCoordinate2DMake(29.545686,106.628259);
//?Ask?Map?Kit?for?a?camera?that?looks?at?the?location?from?an?altitude?of?100?meters?above?the?eye?coordinates.
MKMapCamera*myCamera?=?[MKMapCameracameraLookingAtCenterCoordinate:groundfromEyeCoordinate:eyeeyeAltitude:100];
//?Assign?the?camera?to?your?map?view
mapView.camera=?myCamera;
通過滑動和縮放可以隨時(shí)改變地圖的顯示區(qū)域
? 通過滑動地圖衷戈,改變map view或者camera的centerCoordinate值狭吼,或者直接調(diào)用map view的setCenterCoordinate:animated:或者setCamera:animated:方法
? 改變放大級別,改變map view的region屬性的值殖妇,或者調(diào)用setRegion:animated:方法刁笙,你也可以在3D地圖中改變camera的海拔高度的值(設(shè)置海拔高度為雙倍或者一半,大約等于放大或縮小一個(gè)級別)谦趣。
例如疲吸,下面的代碼將地圖往左移動當(dāng)前地圖寬度的一半的距離,并且?guī)в幸苿拥膭赢嬓Ч?/p>
[objc]view plaincopy
CLLocationCoordinate2D?mapCenter?=?mapView.centerCoordinate;
mapCenter?=?[mapViewconvertPoint:
CGPointMake(1,?(mapView.frame.size.width/2.0))
toCoordinateFromView:mapView];
[mapViewsetCenterCoordinate:mapCenteranimated:YES];
通過修改地圖的region屬性的span的值進(jìn)行地圖縮放蔚润。如果要放大(拉近鏡頭)磅氨,將span設(shè)置為一個(gè)更小的值,如果要縮械站馈(拉遠(yuǎn)鏡頭),將span設(shè)置為一個(gè)更大的值。以下為縮小的示例代碼:
[objc]view plaincopy
MKCoordinateRegion?theRegion?=?myMapView.region;
//?Zoom?out
theRegion.span.longitudeDelta*=2.0;
theRegion.span.latitudeDelta*=2.0;
[myMapViewsetRegion:theRegionanimated:YES];
要在地圖上顯示用戶位置除盏,先設(shè)置map view的showsUserLocation屬性為Yes
[objc]view plaincopy
mapView.showsUserLocation=YES;
以上代碼只是告訴mapView要顯示用戶位置叉橱,但mapView并不知道用戶的位置,因此還需要通過CLLocationManager來使用定位服務(wù):創(chuàng)建一個(gè)CLLocationManager的實(shí)例者蠕,設(shè)置它的desiredAccuracy(期望的定位精度)和distanceFilter(距離過濾)屬性窃祝。為了接收到位置更新的通知,設(shè)置代理CLLocationManagerDelegate踱侣,并調(diào)用startUpdatingLocation方法來注冊接收定位更新(調(diào)用stopUpdatingLocation取消接收定位更新)粪小。另外在iOS8中,需要向用戶請求定位權(quán)限抡句,示例代碼如下:
[objc]view plaincopy
-?(void)startStandardUpdates{
//?Create?the?location?manager?if?this?object?does?not
//?already?have?one.
if(nil==?locationManager)
locationManager?=?[[CLLocationManageralloc]init];
locationManager.delegate=self;
locationManager.desiredAccuracy=?kCLLocationAccuracyBest;
if([[[UIDevicecurrentDevice]systemVersion]floatValue]?>=8.0){
[locationManagerrequestWhenInUseAuthorization];
}
if(![CLLocationManagerlocationServicesEnabled]){
NSLog(@"請開啟定位:設(shè)置?>?隱私?>?位置?>?定位服務(wù)");
}
if([locationManagerrespondsToSelector:@selector(requestAlwaysAuthorization)])?{
[locationManagerrequestAlwaysAuthorization];//?永久授權(quán)
[locationManagerrequestWhenInUseAuthorization];//使用中授權(quán)
}
[locationManagerstartUpdatingLocation];
}
效果如圖:
關(guān)于iOS定位的詳細(xì)講解(后臺持續(xù)定位)探膊,請看我的另一篇博文:http://blog.csdn.net/dolacmeng/article/details/45064939
有時(shí)候,我們的需求并不需要一個(gè)功能完整的map view視圖待榔。例如逞壁,如果app只是想用戶在一個(gè)地圖圖像的滾動列表中選擇,那就不需要實(shí)現(xiàn)地圖的交互功能锐锣。另一個(gè)創(chuàng)建一個(gè)靜態(tài)地圖圖像的原因是為了實(shí)現(xiàn)繪圖功能腌闯。在上面提到的需求中,我們可以使用MKMapSnapshotter對象來異步地創(chuàng)建一個(gè)靜態(tài)的地圖圖像雕憔。
一般的姿骏,通過下面的步驟來創(chuàng)建一個(gè)地圖快照
1.確保網(wǎng)絡(luò)連接正常并且app正在前臺運(yùn)行
2.創(chuàng)建一個(gè)MKMapSnapshotOptions對象,并設(shè)置地圖外觀斤彼、輸出大小等
3.創(chuàng)建一個(gè)MKMapSnapshotter對象并用上一步的實(shí)例對象初始化進(jìn)行初始化分瘦。
4.調(diào)用startWithCompletionHandler:方法來開始異步的快照任務(wù)
5.當(dāng)任務(wù)完成,從block中取回地圖快照畅卓,并添加需要的覆蓋物或者標(biāo)注擅腰。
示例代碼:
[objc]view plaincopy
-(void)shot{
MKMapSnapshotOptions*options?=?[[MKMapSnapshotOptionsalloc]init];
options.region=?mapView.region;
options.size=?mapView.frame.size;
options.scale=?[[UIScreenmainScreen]scale];
MKMapSnapshotter*snapshotter?=?[[MKMapSnapshotteralloc]initWithOptions:options];
//?Initialize?the?semaphore?to?0?because?there?are?no?resources?yet.
dispatch_semaphore_t?snapshotSem?=?dispatch_semaphore_create(0);
//?Get?a?global?queue?(it?doesn't?matter?which?one).
dispatch_queue_t?queue?=?dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
//?Create?variables?to?hold?return?values.?Use?the?__block?modifier?because?these?variables?will?be?modified?inside?a?block.
__blockMKMapSnapshot*mapSnapshot?=nil;
__blockNSError*error?=nil;
//?Start?the?asynchronous?snapshot-creation?task.
[snapshotterstartWithQueue:queue
completionHandler:^(MKMapSnapshot*snapshot,NSError*e)?{
mapSnapshot?=?snapshot;
error?=?e;
//?The?dispatch_semaphore_signal?function?tells?the?semaphore?that?the?async?task?is?finished,?which?unblocks?the?main?thread.
dispatch_semaphore_signal(snapshotSem);
}];
//?On?the?main?thread,?use?dispatch_semaphore_wait?to?wait?for?the?snapshot?task?to?complete.
dispatch_semaphore_wait(snapshotSem,?DISPATCH_TIME_FOREVER);
if(error)?{//?Handle?error.?}
//?Get?the?image?from?the?newly?created?snapshot.
//UIImage?*image?=?mapSnapshot.image;
//?Optionally,?draw?annotations?on?the?image?before?displaying?it.
}
UIImage*image?=?mapSnapshot.image;
UIImageView*imageView?=?[[UIImageViewalloc]initWithFrame:CGRectMake(50,50,200,200)];
imageView.image=?image;
[self.viewaddSubview:imageView];
}
效果如下:
7.添加大頭針(下文中統(tǒng)一稱作標(biāo)注)
(1)為了在地圖上添加標(biāo)注,app必須提供兩個(gè)明確的對象
?一個(gè)annotation 對象, 遵從MKAnnotation代理翁潘,管理標(biāo)注的數(shù)據(jù)(只是存儲標(biāo)注的數(shù)據(jù)趁冈,不涉及視圖)
?一個(gè)annotation view, 用來在地圖上畫annotation的表現(xiàn)形式(用來顯示標(biāo)注數(shù)據(jù)的視圖)
(2)明確了(1)中的要求后,得到添加標(biāo)注的步驟:
<1>使用下面任何一個(gè)對象(annotation 對象)來確定一個(gè)合適的標(biāo)注
?使用MKPointAnnotation類來實(shí)現(xiàn)一個(gè)標(biāo)注拜马,這種類型的標(biāo)注包含顯示在標(biāo)注彈出氣泡標(biāo)題和副標(biāo)題的屬性渗勘。
?定義一個(gè)自定義的遵從MKAnnotation代理的對象,自定義的標(biāo)注可以包含任何我們想存貯的數(shù)據(jù)類型俩莽。詳細(xì)看Defining a Custom Annotation Object
<2>定義一個(gè) annotation view來在屏幕上顯示標(biāo)注的數(shù)據(jù)旺坠。怎么定義annotation view 取決于我們的需求,可以是下面的任何一個(gè):
?如果是要使用標(biāo)準(zhǔn)的大頭針標(biāo)注扮超,創(chuàng)建一個(gè)MKPinAnnotationView類的實(shí)例取刃。
?如果標(biāo)注用自定義的靜態(tài)圖片來展示蹋肮,創(chuàng)建一個(gè)MKAnnotationView類的實(shí)例,設(shè)置它的image屬性為展示的圖片璧疗。
?如果一張靜態(tài)圖片不足夠來展現(xiàn)標(biāo)注坯辩,新建一個(gè)MKAnnotationView的子類,實(shí)現(xiàn)自定義的繪制方法崩侠。
<3>實(shí)現(xiàn)mapView:viewForAnnotation:代理方法漆魔。
這個(gè)方法返回一個(gè)MKAnnotationView類型的視圖,來顯示標(biāo)注的信息却音, 如果不實(shí)現(xiàn)這個(gè)代理方法或者return nil改抡,就會用默認(rèn)的 annotation view進(jìn)行數(shù)據(jù)顯示(也就是顯示紅色大頭針、title系瓢、subtitle阿纤,效果請看下面的例子1)。
<4>使用addAnnotation: (或者 addAnnotations:)方法來添加標(biāo)注到地圖八拱。
上面都是直接從官方文檔翻譯的阵赠,感覺有點(diǎn)晦澀難懂,還是上例子吧肌稻,比較比較容易理解~
(例子1)使用MKPointAnnotation類來作為標(biāo)注清蚀,并使用默認(rèn)的annotation view(不用實(shí)現(xiàn)mapView:viewForAnnotation:方法,或者return nil)爹谭,代碼及效果如下:
[objc]view plaincopy
MKPointAnnotation*annotation0=?[[MKPointAnnotationalloc]init];
[annotation0setCoordinate:CLLocationCoordinate2DMake(29.454686,106.529259)];
[annotation0setTitle:@"重慶理工大學(xué)"];
[annotation0setSubtitle:@"重慶市巴南區(qū)紅光大道69號"];
[mapViewaddAnnotation:annotation0];
(例子2)使用MKPointAnnotation類來作為標(biāo)注(還是用例子1中的代碼)枷邪,并使用默認(rèn)的自定義MKPinAnnotationView,將大頭針設(shè)置為紫色诺凡,設(shè)置氣泡左邊的圖像东揣,并在右邊添加一個(gè)按鈕(實(shí)現(xiàn)View:viewForAnnotation:方法,并返回一個(gè)MKPinAnnotationView實(shí)例)腹泌,在例子1中增加代碼及效果如下:
[objc]view plaincopy
-?(MKAnnotationView*)mapView:(MKMapView*)mapViewviewForAnnotation:(id)annotation{
//?If?the?annotation?is?the?user?location,?just?return?nil.(如果是顯示用戶位置的Annotation,則使用默認(rèn)的藍(lán)色圓點(diǎn))
if([annotationisKindOfClass:[MKUserLocationclass]])
returnnil;
if([annotationisKindOfClass:[MKPointAnnotationclass]])?{
//?Try?to?dequeue?an?existing?pin?view?first.(這里跟UITableView的重用差不多)
MKPinAnnotationView*customPinView?=?(MKPinAnnotationView*)[mapView
dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if(!customPinView){
//?If?an?existing?pin?view?was?not?available,?create?one.
customPinView?=?[[MKPinAnnotationViewalloc]initWithAnnotation:annotation
reuseIdentifier:@"CustomPinAnnotationView"];
}
//iOS9中用pinTintColor代替了pinColor
customPinView.pinColor=?MKPinAnnotationColorPurple;
customPinView.animatesDrop=YES;
customPinView.canShowCallout=YES;
UIButton*rightButton?=?[[UIButtonalloc]initWithFrame:CGRectMake(0,0,80,50)];
rightButton.backgroundColor=?[UIColorgrayColor];
[rightButtonsetTitle:@"查看詳情"forState:UIControlStateNormal];
customPinView.rightCalloutAccessoryView=?rightButton;
//?Add?a?custom?image?to?the?left?side?of?the?callout.(設(shè)置彈出起泡的左面圖片)
UIImageView*myCustomImage?=?[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"myimage"]];
customPinView.leftCalloutAccessoryView=?myCustomImage;
returncustomPinView;
}
returnnil;//返回nil代表使用默認(rèn)樣式
}
為了響應(yīng)“查看詳情”的點(diǎn)擊事件嘶卧,只要實(shí)現(xiàn)以下代理方法:
[objc]view plaincopy
-(void)mapView:(MKMapView*)mapViewannotationView:(MKAnnotationView*)viewcalloutAccessoryControlTapped:(UIControl*)control{
NSLog(@"點(diǎn)擊了查看詳情");
}
(例子3)使用MKPointAnnotation類來作為標(biāo)注(還是用例子1中的代碼),并使用默認(rèn)的自定義MKAnnotationView
凉袱,將大頭針設(shè)置為自定義圖像芥吟,(實(shí)現(xiàn)View:viewForAnnotation:方法,并返回一個(gè)MKAnnotationView實(shí)例)专甩,例1中新增代碼及效果如下:
[objc]view plaincopy
-?(MKAnnotationView*)mapView:(MKMapView*)mapViewviewForAnnotation:(id)annotation
{
//?If?the?annotation?is?the?user?location,?just?return?nil.
if([annotationisKindOfClass:[MKUserLocationclass]])
returnnil;
if([annotationisKindOfClass:[MKPointAnnotationclass]])?{
MKAnnotationView*?aView?=?[[MKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"MKPointAnnotation"];
aView.image=?[UIImageimageNamed:@"myimage"];
aView.canShowCallout=YES;
returnaView;
}
returnnil;
}
(例子4)這個(gè)例子較前面的例子要復(fù)雜些:
1. 使用實(shí)現(xiàn)MKAnnotation代理的自定義對象(MyCustomAnnotation)來作為標(biāo)注(能存儲任意的數(shù)據(jù)钟鸵,例如存儲一家餐廳的名字、地址涤躲、好評率棺耍、距離等屬性,上面三個(gè)例子中的MKPointAnnotation只能存儲title和subtitle种樱,這里的例子定義了兩個(gè)屬性:maintitle和subtitle)蒙袍。
2.使用繼承自MKAnnotationView的類(MyCustomAnnotationView)來自定義氣泡視圖(實(shí)現(xiàn)View:viewForAnnotation:方法俊卤,并返回一個(gè)繼承自MKAnnotationView的實(shí)例,其中左敌,MKAnnotationView包含一個(gè)UIViewController的屬性瘾蛋,這個(gè)view controller用來管理氣泡的視圖和響應(yīng)事件俐镐,并且這個(gè)例子使用xib來設(shè)計(jì)界面)矫限。
3.在MyCustomAnnotationView中要實(shí)現(xiàn)hitTest:和setSelected:animated:方法,來顯示或隱藏氣泡佩抹。
代碼及效果如下:
MyCustomAnnotation.h文件
[objc]view plaincopy
#import?
#import?
@interfaceMyCustomAnnotation?:?NSObject??{
CLLocationCoordinate2D?coordinate;
}
@property(nonatomic,readonly)?CLLocationCoordinate2D?coordinate;
@property(nonatomic,strong)NSString*maintitle;
@property(nonatomic,strong)NSString*secondtitle;
-?(id)initWithLocation:(CLLocationCoordinate2D)coord;
@end
MyCustomAnnotation.m文件
[objc]view plaincopy
#import?"MyCustomAnnotation.h"
@implementationMyCustomAnnotation
@synthesizecoordinate;
-?(id)initWithLocation:(CLLocationCoordinate2D)coord?{
self=?[superinit];
if(self)?{
coordinate?=?coord;
}
returnself;
}
@end
MyCustomAnnotationView.h
[objc]view plaincopy
#import?
#import?"CustomCalloutViewController.h"
@classMyCustomAnnotation;
@interfaceMyCustomAnnotationView?:?MKPinAnnotationView
-?(id)initWithAnnotation:(id)annotationreuseIdentifier:(NSString*)reuseIdentifier;
@property(strong,nonatomic)CustomCalloutViewController*calloutViewController;
@property(strong,nonatomic)MyCustomAnnotation*myCustomAnnotation;
@end
MyCustomAnnotationView.m
[objc]view plaincopy
#import?"MyCustomAnnotationView.h"
#import?"MyCustomAnnotation.h"
@implementationMyCustomAnnotationView
-?(id)initWithAnnotation:(id)annotationreuseIdentifier:(NSString*)reuseIdentifier
{
self=?[superinitWithAnnotation:annotationreuseIdentifier:reuseIdentifier];
if(self)
{
//?Set?the?frame?size?to?the?appropriate?values.
CGRect??myFrame?=self.frame;
myFrame.size.width=20;
myFrame.size.height=20;
self.frame=?myFrame;
//?The?opaque?property?is?YES?by?default.?Setting?it?to
//?NO?allows?map?content?to?show?through?any?unrendered?parts?of?your?view.
self.opaque=NO;
self.canShowCallout=NO;
self.calloutViewController=?[[CustomCalloutViewControlleralloc]initWithNibName:@"CustomCalloutView"bundle:nil];
self.myCustomAnnotation=?(MyCustomAnnotation*)annotation;
}
returnself;
}
-?(UIView*)hitTest:(CGPoint)pointwithEvent:(UIEvent*)event
{
UIView*hitView?=?[superhitTest:pointwithEvent:event];
if(hitView?==nil&&self.selected)?{
CGPoint?pointInAnnotationView?=?[self.superviewconvertPoint:pointtoView:self];
UIView*calloutView?=self.calloutViewController.view;
hitView?=?[calloutViewhitTest:pointInAnnotationViewwithEvent:event];
}
returnhitView;
}
-?(void)setSelected:(BOOL)selectedanimated:(BOOL)animated
{
[supersetSelected:selectedanimated:YES];
//?Get?the?custom?callout?view.
UIView*calloutView?=self.calloutViewController.view;
if(selected)?{
self.calloutViewController.maintitle.text=self.myCustomAnnotation.maintitle;
self.calloutViewController.secondtitle.text=self.myCustomAnnotation.secondtitle;
CGRect?annotationViewBounds?=self.bounds;
CGRect?calloutViewFrame?=?calloutView.frame;
if(calloutViewFrame.origin.x==annotationViewBounds.origin.x)?{
//?Center?the?callout?view?above?and?to?the?right?of?the?annotation?view.
calloutViewFrame.origin.x-=?(calloutViewFrame.size.width-?annotationViewBounds.size.width)?*0.5;
calloutViewFrame.origin.y-=?(calloutViewFrame.size.height);
calloutView.frame=?calloutViewFrame;
}
[selfaddSubview:calloutView];
}else{
[calloutViewremoveFromSuperview];
}
}
@end
CustomCalloutViewController.h文件
[objc]view plaincopy
@interfaceCustomCalloutViewController?:?UIViewController{
UILabel*labTitle;
}
@property(weak,nonatomic)?IBOutletUILabel*maintitle;
@property(weak,nonatomic)?IBOutletUILabel*secondtitle;
@end
CustomCalloutViewController.m文件
[objc]view plaincopy
@implementationCustomCalloutViewController
-?(void)viewDidLoad?{
[superviewDidLoad];
}
-?(void)didReceiveMemoryWarning?{
[superdidReceiveMemoryWarning];
}
@end
View:viewForAnnotation:代理方法:
[objc]view plaincopy
-?(MKAnnotationView*)mapView:(MKMapView*)mapView
viewForAnnotation:(id)annotation
{
????MyCustomAnnotationView*annotationView?=?(MyCustomAnnotationView*)[mapViewdequeueReusableAnnotationViewWithIdentifier:@"MyCustomAnnotationView"];
if(!annotationView)?{
annotationView?=?[[MyCustomAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"MyCustomAnnotationView"];
annotationView.image=?[UIImageimageNamed:@"myimage"];
}
returnannotationView;
}
另外叼风,還能實(shí)現(xiàn)導(dǎo)航等功能,有時(shí)間再繼續(xù)寫棍苹。