導(dǎo)入系統(tǒng)庫(kù)
通過(guò)拖控件的方式:
代碼實(shí)現(xiàn)
導(dǎo)入頭文件
#import< MapKit/MapKit.h>//地圖
#import <CoreLocation/CoreLocation.h>//定位
添加 ?<MKMapViewDelegate > 協(xié)議
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (weak, nonatomic) IBOutlet UITextField *weiduTf;
@property (weak, nonatomic) IBOutlet UITextField *jingduTf;
//地理編碼
@property(strong,nonatomic)CLGeocoder *geocoder;
- (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è)置顯示用戶(hù)當(dāng)前位置
self.mapView.showsUserLocation=YES;
//調(diào)用自己實(shí)現(xiàn)的方法設(shè)置地圖的顯示位置和顯示區(qū)域
[self locateToLatitude:37.23 longitude:122.1234];
//創(chuàng)建手勢(shì)對(duì)象 ?(覆蓋物手勢(shì))
UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self? ? action:@selector(tapAction:)];
//配置屬性
//輕拍次數(shù)
tap.numberOfTapsRequired =1;
//輕拍手指
tap.numberOfTouchesRequired =1;
//添加到視圖
[self.view addGestureRecognizer:tap];
// 創(chuàng)建一個(gè)手勢(shì)處理器馍管,用于檢測(cè)、處理長(zhǎng)按手勢(shì)(錨點(diǎn)手勢(shì))
UILongPressGestureRecognizer* gesture = [[UILongPressGestureRecognizer ? ? ? alloc]initWithTarget:self action:@selector(longPress:)];
//為該控件添加手勢(shì)處理器
[self.view addGestureRecognizer:gesture];
//遵守代理是要實(shí)現(xiàn)自定義錨點(diǎn)
self.mapView.delegate=self;
}
按鈕方法
- (IBAction)chazhao:(id)sender {
//緯度
NSString* latitudeStr =self.weiduTf.text;
//經(jīng)度
NSString* longtitudeStr =self.jingduTf.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];
}
}
手勢(shì)方法
#pragma mark --手勢(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];
}
}];
}
# pragma? ? 點(diǎn)按手勢(shì)回調(diào)
//輕拍事件
-(void)tapAction:(UITapGestureRecognizer *)tap
{
// 獲取長(zhǎng)按點(diǎn)的坐標(biāo)
CGPoint pos = [tap locationInView:self.mapView];
// 將長(zhǎng)按點(diǎn)的坐標(biāo)轉(zhuǎn)換為經(jīng)度肴熏、維度值
CLLocationCoordinate2D coord = [self.mapView convertPoint:pos
toCoordinateFromView:self.mapView];
// 創(chuàng)建MKCircle對(duì)象鬼雀,該對(duì)象代表覆蓋層
MKCircle* circle = [MKCircle circleWithCenterCoordinate:coord radius:100];
// 添加MKOverlay
[self.mapView addOverlay:circle level:MKOverlayLevelAboveLabels];
}
自定義封裝定位方法
- (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=@"北京石羿科技發(fā)展有限公司";
annotation.subtitle=@"海淀區(qū)中關(guān)村軟件園";
CLLocationCoordinate2D coordinate = {latitude , longitude};
annotation.coordinate= coordinate;
//添加錨點(diǎn)
[self.mapView addAnnotation:annotation];
}
#pragma mark -自定義錨點(diǎn)
// 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:@"1.png"];
//設(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;
}
#pragma mark -自定義錨點(diǎn)--里面的詳情按鈕
- (void) buttonTapped:(id)sender
{
NSLog(@"您點(diǎn)擊了錨點(diǎn)信息!");
}
// MKMapViewDelegate協(xié)議中的方法矢炼,該方法返回的MKOverlayRenderer負(fù)責(zé)繪制覆蓋層控件- (MKOverlayRenderer *)mapView:(MKMapView *)mapViewrendererForOverlay:(id)overlay
{
MKCircle * circle = (MKCircle*)overlay;
// 創(chuàng)建一個(gè)MKCircleRenderer對(duì)象
MKCircleRenderer* render = [[MKCircleRenderer alloc] initWithCircle:circle];
// 設(shè)置MKCircleRenderer的透明度
render.alpha = 0.5;
// 設(shè)置MKCircleRenderer的填充顏色和邊框顏色
render.fillColor = [UIColor blueColor];
render.strokeColor = [UIColor redColor];
return render;
}
最終效果
覆蓋物效果