需在Connection中選中Action錨點(diǎn)針對于地圖定位功能,就是我們?nèi)粘I钪械貓D定位的小圖標(biāo),為我們的出行提供和了極大方便,現(xiàn)在就為大家介紹一下在ios開發(fā)中錨點(diǎn)的使用方法與步驟(此代碼使用的是固定的位置,可針對其需要加以具體的修改)
一档叔、 在ViewController.m中導(dǎo)入我們所需要的頭文件
#import <MapKit/MapKit.h>//地圖
#import <CoreLocation/CoreLocation.h>//定位
二牌废、 對頁面進(jìn)行布局,拖拽控件并將我們所要用到的控件與ViewController進(jìn)行關(guān)聯(lián) 具體如下圖所示
屏幕快照 2017-08-03 下午7.09.19.png
注意: 在進(jìn)行控件的拖拽時(shí) 我們要掌握先后的次序,先拖拽Map View 控件,并將它不滿全屏,其次在將 lable Text Field Button 放在Map View 上,在關(guān)聯(lián)button時(shí)需在Connection中選中Action再進(jìn)行關(guān)聯(lián)
具體關(guān)聯(lián)后的效果 如下圖所示
屏幕快照 2017-08-03 下午7.14.39.png
三、因?yàn)槲覀円獙?shí)現(xiàn)的功能有手勢的需要 所以我們要為程序 寫一下手勢的代碼并將地圖的一個必要功能實(shí)現(xiàn)在.m中
- (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è)置地圖可滾動
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:37.23 longitude:122.1234];
// 創(chuàng)建一個手勢處理器 用于檢測處理 長按手勢
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
// 為該控件 添加手勢處理器
[self.view addGestureRecognizer:gesture];
// 設(shè)置代理方法
self.mapView.delegate = self;
}
四 倾芝、接下來我們要對手勢的回調(diào)寫具體的代碼
#pragma mark -手勢回調(diào)
-(void)longPress:(UILongPressGestureRecognizer *)getsure
{
// 獲取長按點(diǎn)的坐標(biāo)
CGPoint pos = [getsure locationInView:self.mapView];
// 把獲取到的坐標(biāo)轉(zhuǎn)換成經(jīng)緯度
CLLocationCoordinate2D coord = [self.mapView convertPoint:pos toCoordinateFromView:self.mapView];
// 再把經(jīng)緯度值添加到 CLLocation(定位)
CLLocation *location = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
// 根據(jù)經(jīng)緯度反向解析地址
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error)
{
if (placemarks.count >0 && error == nil)
{
// 獲取解析得到的第一個地址信息
CLPlacemark *placemark = placemarks[0];
// 獲取地址信息中的FormattedAddressLines對應(yīng)的詳細(xì)地址
NSArray *addrArray = placemark.addressDictionary[@"FormattedAddressLines"];
// 將詳細(xì)地址拼接成一個字符串
NSMutableString *address = [[NSMutableString alloc] init];
for (int i = 0; i < addrArray.count; i++)
{
[address appendString:addrArray[i]];
}
// 創(chuàng)建錨點(diǎn)(MKPointAnnotation)
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
// 設(shè)置標(biāo)題
annotation.title = placemark.name;
// 設(shè)置子標(biāo)題
annotation.subtitle = address;
// 把坐標(biāo)放入錨點(diǎn)里面
annotation.coordinate = coord;
// 添加錨點(diǎn)
[self.mapView addAnnotation:annotation];
}
}];
}
五、點(diǎn)擊回到輸入的經(jīng)緯度位置
- (IBAction)goClicked:(id)sender
{
// 關(guān)閉兩個文本框的虛擬鍵盤
[self.latitudeField resignFirstResponder];
[self.longitudeField resignFirstResponder];
//緯度
NSString* latitudeStr = self.latitudeField.text;
//經(jīng)度
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];
}
}
六、對一些方法進(jìn)行封裝進(jìn)行以后的調(diào)用
- (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對象挫望,該對象代表了地圖的顯示中心和顯示范圍。
MKCoordinateRegion region = {center,span};
// 設(shè)置當(dāng)前地圖的顯示中心和顯示范圍
[self.mapView setRegion:region animated:YES];
// 創(chuàng)建MKPointAnnotation對象——代表一個錨點(diǎn)
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
annotation.title = @"北京石羿科技發(fā)展有限公司";
annotation.subtitle = @"海淀區(qū)中關(guān)村軟件園";
CLLocationCoordinate2D coordinate = {latitude , longitude};
annotation.coordinate = coordinate;
// 添加錨點(diǎn)
[self.mapView addAnnotation:annotation];
}
七狂窑、對錨點(diǎn)的外觀進(jìn)行具體的設(shè)置
// MKMapViewDelegate協(xié)議中的方法媳板,該方法的返回值可用于定制錨點(diǎn)控件的外觀
- (MKAnnotationView *) mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>) 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對象的pinColor屬性即可。
*/
}
// 為錨點(diǎn)控件設(shè)置圖片
annoView.image = [UIImage imageNamed:@"pos.gif"];
// 設(shè)置該錨點(diǎn)控件是否可顯示氣泡信息
annoView.canShowCallout = YES;
// 定義一個按鈕丛晦,用于為錨點(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;
}
- (void) buttonTapped:(id)sender
{
NSLog(@"您點(diǎn)擊了錨點(diǎn)信息!");
}
最后可在打印欄中看到以上代碼的信息