高德地圖《錨點(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è)手勢(shì)處理器,用于檢測(cè)、處理長(zhǎng)按手勢(shì)

UILongPressGestureRecognizer* gesture = [[UILongPressGestureRecognizer

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

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

[self.view addGestureRecognizer:gesture];

self.mapView.delegate = self;

}

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

// 如果用戶輸入的經(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

{

// 獲取長(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 *)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];

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

annoView.rightCalloutAccessoryView = button;

return annoView;

}

#endif

- (void) buttonTapped:(id)sender

{

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

}

@end

作者:Cyy

鏈接:http://www.itdecent.cn/p/0fa1acf632de

來(lái)源:簡(jiǎn)書(shū)

著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)繫作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)?jiān)]明出處。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容