//聯系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄
/**
注意點: 1.看 GIF 效果圖.
2.看連線視圖的效果圖.
3.看實現代碼(直接復制實現效果).
*/
一、GIF 效果圖:
二、連線視圖的效果圖:
圖1:
圖2:
三、實現代碼:
=========================
===================================================
==========================
/**
注意點: 1.自定義錨點定位圖片(pos.gif
)--->拷貝到桌面-->在拷貝到項目中 :
*/
//
//? ViewController.m
//地圖錨點(MapAnchor)
//
//? Created by石虎on 2017/7/5.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import"ViewController.h"
#import<MapKit/MapKit.h>//地圖
#import<CoreLocation/CoreLocation.>//定位
@interfaceViewController()
//返回當前的位置
- (IBAction)goClicked:(id)sender;
//緯度
@property(strong,nonatomic)IBOutletUITextField*latitudeField;
//緯度
@property(strong,nonatomic)IBOutletUITextField*longitudeField;
//地圖
@property(strong,nonatomic)IBOutletMKMapView*mapView;
//地理編碼
@property(strong,nonatomic)CLGeocoder*geocoder;
@end
@implementationViewController
- (void)viewDidLoad{
[superviewDidLoad];
//初始化地理編碼
_geocoder= [[CLGeocoderalloc]init];
//設置地圖的顯示風格,此處設置使用標準地圖
self.mapView.mapType=MKMapTypeStandard;
//設置地圖可縮放
self.mapView.zoomEnabled=YES;
//設置地圖可滾動
self.mapView.scrollEnabled=YES;
//設置地圖可旋轉
self.mapView.rotateEnabled=YES;
//設置顯示用戶當前位置
self.mapView.showsUserLocation=YES;
//調用自己實現的方法設置地圖的顯示位置和顯示區域
[selflocateToLatitude:37.23longitude:122.1234];
//創建一個手勢處理器,用于檢測、處理長按手勢
UILongPressGestureRecognizer* gesture = [[UILongPressGestureRecognizer
alloc]initWithTarget:selfaction:@selector(longPress:)];
//為該控件添加手勢處理器
[self.viewaddGestureRecognizer:gesture];
//遵守代理是要實現自定義錨點
self.mapView.delegate=self;
}
#pragma mark --手勢回調
- (void) longPress:(UILongPressGestureRecognizer*)gesture{
//獲取長按點的坐標
CGPointpos = [gesturelocationInView:self.mapView];
//將長按點的坐標轉換為經度、維度值
CLLocationCoordinate2Dcoord = [self.mapViewconvertPoint:pos
toCoordinateFromView:self.mapView];
//將經度、維度值包裝為CLLocation對象
CLLocation* location = [[CLLocationalloc]initWithLatitude:coord.latitude
longitude:coord.longitude];
//根據經、緯度反向解析地址
[_geocoderreverseGeocodeLocation:locationcompletionHandler:
^(NSArray*placemarks,NSError*error)
{
if(placemarks.count>0&& error ==nil)
{
//獲取解析得到的第一個地址信息
CLPlacemark* placemark = [placemarksobjectAtIndex:0];
//獲取地址信息中的FormattedAddressLines對應的詳細地址
NSArray* addrArray = placemark
.addressDictionary[@"FormattedAddressLines"];
//將詳細地址拼接成一個字符串
NSMutableString* address = [[NSMutableStringalloc]init];
for(inti =0; i < addrArray.count; i ++)
{
[addressappendString:addrArray[i]];
}
//創建MKPointAnnotation對象——代表一個錨點
MKPointAnnotation*annotation = [[MKPointAnnotationalloc]init];
annotation.title= placemark.name;
annotation.subtitle= address;
annotation.coordinate= coord;
//添加錨點
[self.mapViewaddAnnotation:annotation];
}
}];
}
#pragma mark -點擊回到輸入的的經緯度位置
- (IBAction)goClicked:(id)sender{
//關閉兩個文本框的虛擬鍵盤
[self.latitudeFieldresignFirstResponder];
[self.longitudeFieldresignFirstResponder];
//緯度
NSString* latitudeStr =self.latitudeField.text;
//經度
NSString* longtitudeStr =self.longitudeField.text;
//如果用戶輸入的經度、緯度不為空
if(latitudeStr !=nil&& latitudeStr.length>0
&& longtitudeStr !=nil&& longtitudeStr.length>0)
{
//調用自己實現的方法設置地圖的顯示位置和顯示區域
[selflocateToLatitude:latitudeStr.floatValue
longitude:longtitudeStr.floatValue];
}
}
#pragma mark --自定義封裝定位方法
- (void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{
//設置地圖中心的經、緯度
CLLocationCoordinate2Dcenter = {latitude , longitude};
//設置地圖顯示的范圍,
MKCoordinateSpanspan;
//地圖顯示范圍越小,細節越清楚
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
//創建MKCoordinateRegion對象,該對象代表了地圖的顯示中心和顯示范圍。
MKCoordinateRegionregion = {center,span};
//設置當前地圖的顯示中心和顯示范圍
[self.mapViewsetRegion:regionanimated:YES];
//創建MKPointAnnotation對象——代表一個錨點
MKPointAnnotation* annotation = [[MKPointAnnotationalloc]init];
annotation.title=@"北京石羿科技發展有限公司";
annotation.subtitle=@"海淀區中關村軟件園";
CLLocationCoordinate2Dcoordinate = {latitude , longitude};
annotation.coordinate= coordinate;
//添加錨點
[self.mapViewaddAnnotation:annotation];
}
#pragma mark -自定義錨點
// MKMapViewDelegate協議中的方法,該方法的返回值可用于定制錨點控件的外觀
- (MKAnnotationView*) mapView:(MKMapView*)mapView
viewForAnnotation:(id) annotation{
staticNSString* annoId =@"fkAnno";
//獲取可重用的錨點控件
MKAnnotationView* annoView = [mapView
dequeueReusableAnnotationViewWithIdentifier:annoId];
//如果可重用的錨點控件不存在,創建新的可重用錨點控件
if(!annoView)
{
annoView= [[MKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:annoId];
/*
如果不想改變錨點控件的圖片,只想改變顏色,則可創建MKPinAnnotationView實例
再修改MKPinAnnotationView對象的pinColor屬性即可。
*/
}
//為錨點控件設置圖片
annoView.image= [UIImageimageNamed:@"pos.gif"];
//設置該錨點控件是否可顯示氣泡信息
annoView.canShowCallout=YES;
//定義一個按鈕,用于為錨點控件設置附加控件
UIButton*button = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];
//為按鈕綁定事件處理方法
[buttonaddTarget:selfaction:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
//可通過錨點控件的rightCalloutAccessoryView、leftCalloutAccessoryView設置附加控件
annoView.rightCalloutAccessoryView= button;
returnannoView;
}
#pragma mark -自定義錨點--里面的詳情按鈕
- (void) buttonTapped:(id)sender
{
NSLog(@"您點擊了錨點信息!");
}
@end