地圖定位與錨點(diǎn)

記得加入庫(kù)MapKit

#import

#import

{

CLGeocoder* geocoder;

}

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

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

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

- (IBAction)goClicked:(id)sender;

//================

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è)置顯示用戶(hù)當(dāng)前位置

self.mapView.showsUserLocation = YES;

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

[self locateToLatitude:39.5427 longitude:116.2317];

// 創(chuàng)建一個(gè)手勢(shì)處理器威鹿,用于檢測(cè)囊卜、處理長(zhǎng)按手勢(shì)

UILongPressGestureRecognizer* gesture = [[UILongPressGestureRecognizer

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

// 為該控件添加手勢(shì)處理器

[self.view addGestureRecognizer:gesture];

//=============

- (IBAction)goClicked:(id)sender

{

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

[self.latitudeField resignFirstResponder];

[self.longitudeField resignFirstResponder];

NSString* latitudeStr = self.latitudeField.text;

NSString* longtitudeStr = self.longitudeField.text;

// 如果用戶(hù)輸入的經(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];

}

}

//自定義

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

}

//手勢(shì)回調(diào)

- (void)longPress:(UILongPressGestureRecognizer*)gesture

{

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

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

// 將長(zhǎng)按點(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 *)mapView? ? ? ? ? ?? viewForAnnotation:(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];

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

annoView.rightCalloutAccessoryView = button;

return annoView;

}

#endif

- (void) buttonTapped:(id)sender

{

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

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鲤嫡,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子绑莺,更是在濱河造成了極大的恐慌暖眼,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,681評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件纺裁,死亡現(xiàn)場(chǎng)離奇詭異诫肠,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)欺缘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)栋豫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人谚殊,你說(shuō)我怎么就攤上這事丧鸯。” “怎么了嫩絮?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,421評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵丛肢,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我剿干,道長(zhǎng)摔踱,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,114評(píng)論 1 300
  • 正文 為了忘掉前任怨愤,我火速辦了婚禮,結(jié)果婚禮上蛹批,老公的妹妹穿的比我還像新娘撰洗。我一直安慰自己,他們只是感情好腐芍,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,116評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布差导。 她就那樣靜靜地躺著,像睡著了一般猪勇。 火紅的嫁衣襯著肌膚如雪设褐。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,713評(píng)論 1 312
  • 那天泣刹,我揣著相機(jī)與錄音助析,去河邊找鬼。 笑死椅您,一個(gè)胖子當(dāng)著我的面吹牛外冀,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播掀泳,決...
    沈念sama閱讀 41,170評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼雪隧,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼西轩!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起脑沿,我...
    開(kāi)封第一講書(shū)人閱讀 40,116評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤藕畔,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后庄拇,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體注服,經(jīng)...
    沈念sama閱讀 46,651評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,714評(píng)論 3 342
  • 正文 我和宋清朗相戀三年丛忆,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了祠汇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,865評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡熄诡,死狀恐怖可很,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情凰浮,我是刑警寧澤我抠,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站袜茧,受9級(jí)特大地震影響菜拓,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜笛厦,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,211評(píng)論 3 336
  • 文/蒙蒙 一纳鼎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧裳凸,春花似錦贱鄙、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,699評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至梦湘,卻和暖如春瞎颗,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背捌议。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,814評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工哼拔, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瓣颅。 一個(gè)月前我還...
    沈念sama閱讀 49,299評(píng)論 3 379
  • 正文 我出身青樓管挟,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親弄捕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子僻孝,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,870評(píng)論 2 361

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