iOS Mapkit的使用

【iOS】Mapkit的使用:地圖顯示泼各、定位、大頭針晨另、氣泡等

標(biāo)簽:iOS地圖mapkit

1.顯示地圖

(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)行效果如下:

2.設(shè)置地圖的默認(rèn)顯示區(qū)域

步驟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;

4.滑動和縮放地圖內(nèi)容

通過滑動和縮放可以隨時(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];

5.在地圖上顯示用戶當(dāng)前位置

要在地圖上顯示用戶位置除盏,先設(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

6.創(chuàng)建一個(gè)地圖快照

有時(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ù)寫棍苹。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末无宿,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子枢里,更是在濱河造成了極大的恐慌孽鸡,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件栏豺,死亡現(xiàn)場離奇詭異彬碱,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)奥洼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門巷疼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人灵奖,你說我怎么就攤上這事嚼沿。” “怎么了瓷患?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵骡尽,是天一觀的道長。 經(jīng)常有香客問我擅编,道長攀细,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任沙咏,我火速辦了婚禮辨图,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘肢藐。我一直安慰自己故河,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布吆豹。 她就那樣靜靜地躺著鱼的,像睡著了一般理盆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上凑阶,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天猿规,我揣著相機(jī)與錄音,去河邊找鬼宙橱。 笑死姨俩,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的师郑。 我是一名探鬼主播环葵,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼宝冕!你這毒婦竟也來了张遭?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤地梨,失蹤者是張志新(化名)和其女友劉穎菊卷,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宝剖,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡洁闰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了诈闺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片渴庆。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖雅镊,靈堂內(nèi)的尸體忽然破棺而出襟雷,到底是詐尸還是另有隱情,我是刑警寧澤仁烹,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布耸弄,位于F島的核電站,受9級特大地震影響卓缰,放射性物質(zhì)發(fā)生泄漏计呈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一征唬、第九天 我趴在偏房一處隱蔽的房頂上張望捌显。 院中可真熱鬧,春花似錦总寒、人聲如沸扶歪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽善镰。三九已至妹萨,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間炫欺,已是汗流浹背乎完。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留品洛,地道東北人树姨。 一個(gè)月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像毫别,于是被迫代替她去往敵國和親娃弓。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容