地圖應(yīng)用3《高德地圖錨點(diǎn)》

ViewController。h托控件視圖展示

@property (strong, nonatomic) IBOutlet UITextField *latitudeField;

@property (strong, nonatomic) IBOutlet UITextField *longitudeField;

@property (strong, nonatomic) IBOutlet MKMapView *mapView;

- (IBAction)goClicked:(id)sender;


ViewController匙隔。m 具體實(shí)現(xiàn)

#import#import "FKViewController.h"@interface FKViewController (){

//

CLGeocoder* geocoder;

}

@end

@implementation FKViewController

- (void)viewDidLoad

{

[super viewDidLoad];

geocoder = [[CLGeocoder alloc] init];

// 設(shè)置地圖的顯示風(fēng)格涉馅,此處設(shè)置使用標(biāo)準(zhǔn)地圖

self.mapView.mapType = MKMapTypeStandard;

// 設(shè)置地圖可縮放

self.mapView.zoomEnabled = YES;

// 設(shè)置地圖可滾動(dòng)

self.mapView.scrollEnabled = YES;

// 設(shè)置地圖可旋轉(zhuǎn)

self.mapView.rotateEnabled = YES;

// 設(shè)置顯示用戶當(dāng)前位置

self.mapView.showsUserLocation = YES;

// 調(diào)用自己實(shí)現(xiàn)的方法設(shè)置地圖的顯示位置和顯示區(qū)域

[self locateToLatitude:39.5427 longitude:116.2317];

// 創(chuàng)建一個(gè)手勢處理器般贼,用于檢測儒喊、處理長按手勢

UILongPressGestureRecognizer* gesture = [[UILongPressGestureRecognizer

alloc]initWithTarget:self action:@selector(longPress:)];

// 為該控件添加手勢處理器

[self.view addGestureRecognizer:gesture];

self.mapView.delegate = self;

}

- (IBAction)goClicked:(id)sender

{

// 關(guān)閉兩個(gè)文本框的虛擬鍵盤

[self.latitudeField resignFirstResponder];

[self.longitudeField resignFirstResponder];

NSString* latitudeStr = self.latitudeField.text;

NSString* longtitudeStr = self.longitudeField.text;

// 如果用戶輸入的經(jīng)度祠肥、緯度不為空

if (latitudeStr != nil && latitudeStr.length > 0

&& longtitudeStr != nil && longtitudeStr.length > 0)

{

// 調(diào)用自己實(shí)現(xiàn)的方法設(shè)置地圖的顯示位置和顯示區(qū)域

[self locateToLatitude:latitudeStr.floatValue

longitude:longtitudeStr.floatValue];

}

}

//自定義方法跳轉(zhuǎn)至指定經(jīng)緯度并添加錨點(diǎn)

- (void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude

{

// 設(shè)置地圖中心的經(jīng)纸镊、緯度

CLLocationCoordinate2D center = {latitude , longitude};

// 設(shè)置地圖顯示的范圍鸵闪,

MKCoordinateSpan span;

// 地圖顯示范圍越小檐晕,細(xì)節(jié)越清楚

span.latitudeDelta = 0.01;

span.longitudeDelta = 0.01;

// 創(chuàng)建MKCoordinateRegion對(duì)象,該對(duì)象代表了地圖的顯示中心和顯示范圍蚌讼。

MKCoordinateRegion region = {center,span};

// 設(shè)置當(dāng)前地圖的顯示中心和顯示范圍

[self.mapView setRegion:region animated:YES];

// 創(chuàng)建MKPointAnnotation對(duì)象——代表一個(gè)錨點(diǎn)

MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];

annotation.title = @"八維研修學(xué)院";

annotation.subtitle = @"上地7街軟件園南";

CLLocationCoordinate2D coordinate = {latitude , longitude};

annotation.coordinate = coordinate;

// 添加錨點(diǎn)

[self.mapView addAnnotation:annotation];

}

- (void) longPress:(UILongPressGestureRecognizer*)gesture

{

// 獲取長按點(diǎn)的坐標(biāo)

CGPoint pos = [gesture locationInView:self.mapView];

// 將長按點(diǎn)的坐標(biāo)轉(zhuǎn)換為經(jīng)度辟灰、維度值

CLLocationCoordinate2D coord = [self.mapView convertPoint:pos

toCoordinateFromView:self.mapView];

// 將經(jīng)度、維度值包裝為CLLocation對(duì)象

CLLocation* location = [[CLLocation alloc] initWithLatitude:coord.latitude

longitude:coord.longitude];

// 根據(jù)經(jīng)篡石、緯度反向解析地址

[geocoder reverseGeocodeLocation:location completionHandler:

^(NSArray *placemarks, NSError *error)

{

if (placemarks.count > 0 && error == nil)

{

// 獲取解析得到的第一個(gè)地址信息

CLPlacemark* placemark = [placemarks objectAtIndex:0];

// 獲取地址信息中的FormattedAddressLines對(duì)應(yīng)的詳細(xì)地址

NSArray* addrArray = placemark

.addressDictionary[@"FormattedAddressLines"];

// 將詳細(xì)地址拼接成一個(gè)字符串

NSMutableString* address = [[NSMutableString alloc] init];

for(int i = 0 ; i < addrArray.count ; i ++)

{

[address appendString:addrArray[i]];

}

// 創(chuàng)建MKPointAnnotation對(duì)象——代表一個(gè)錨點(diǎn)

MKPointAnnotation? *annotation = [[MKPointAnnotation alloc] init];

annotation.title = placemark.name;

annotation.subtitle = address;

annotation.coordinate = coord;

// 添加錨點(diǎn)

[self.mapView addAnnotation:annotation];

}

}];

}

#if 1

// MKMapViewDelegate協(xié)議中的方法芥喇,該方法的返回值可用于定制錨點(diǎn)控件的外觀

- (MKAnnotationView *) mapView:(MKMapView *)mapViewviewForAnnotation:(id) annotation

{

static NSString* annoId = @"fkAnno";

// 獲取可重用的錨點(diǎn)控件

MKAnnotationView* annoView = [mapView

dequeueReusableAnnotationViewWithIdentifier:annoId];

// 如果可重用的錨點(diǎn)控件不存在,創(chuàng)建新的可重用錨點(diǎn)控件

if (!annoView)

{

annoView= [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoId];

/*

如果不想改變錨點(diǎn)控件的圖片凰萨,只想改變顏色继控,則可創(chuàng)建MKPinAnnotationView實(shí)例

再修改MKPinAnnotationView對(duì)象的pinColor屬性即可。

*/

}

// 為錨點(diǎn)控件設(shè)置圖片

annoView.image = [UIImage imageNamed:@"pos.gif"];

// 設(shè)置該錨點(diǎn)控件是否可顯示氣泡信息

annoView.canShowCallout = YES;

// 定義一個(gè)按鈕胖眷,用于為錨點(diǎn)控件設(shè)置附加控件

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

// 為按鈕綁定事件處理方法

[button addTarget:self action:@selector(buttonTapped:)

forControlEvents:UIControlEventTouchUpInside];

// 可通過錨點(diǎn)控件的rightCalloutAccessoryView武通、leftCalloutAccessoryView設(shè)置附加控件

annoView.rightCalloutAccessoryView = button;

return annoView;

}

#endif

- (void) buttonTapped:(id)sender

{

NSLog(@"您點(diǎn)擊了錨點(diǎn)信息!");

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末珊搀,一起剝皮案震驚了整個(gè)濱河市冶忱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌境析,老刑警劉巖囚枪,帶你破解...
    沈念sama閱讀 216,470評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異劳淆,居然都是意外死亡链沼,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門沛鸵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來忆植,“玉大人,你說我怎么就攤上這事〕” “怎么了?”我有些...
    開封第一講書人閱讀 162,577評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵蜈缤,是天一觀的道長拾氓。 經(jīng)常有香客問我,道長底哥,這世上最難降的妖魔是什么咙鞍? 我笑而不...
    開封第一講書人閱讀 58,176評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮趾徽,結(jié)果婚禮上续滋,老公的妹妹穿的比我還像新娘。我一直安慰自己孵奶,他們只是感情好疲酌,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著了袁,像睡著了一般朗恳。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上载绿,一...
    開封第一講書人閱讀 51,155評(píng)論 1 299
  • 那天粥诫,我揣著相機(jī)與錄音,去河邊找鬼崭庸。 笑死怀浆,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的怕享。 我是一名探鬼主播执赡,決...
    沈念sama閱讀 40,041評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼熬粗!你這毒婦竟也來了搀玖?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,903評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤驻呐,失蹤者是張志新(化名)和其女友劉穎灌诅,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體含末,經(jīng)...
    沈念sama閱讀 45,319評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡猜拾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了佣盒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片挎袜。...
    茶點(diǎn)故事閱讀 39,703評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出盯仪,到底是詐尸還是另有隱情紊搪,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評(píng)論 5 343
  • 正文 年R本政府宣布全景,位于F島的核電站耀石,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏爸黄。R本人自食惡果不足惜滞伟,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望炕贵。 院中可真熱鬧梆奈,春花似錦、人聲如沸称开。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽钥弯。三九已至径荔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間脆霎,已是汗流浹背总处。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留睛蛛,地道東北人鹦马。 一個(gè)月前我還...
    沈念sama閱讀 47,711評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像忆肾,于是被迫代替她去往敵國和親荸频。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評(píng)論 2 353

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