#import <MapKit/MapKit.h>
#import "ViewController.h"
@interface ViewController ()<MKMapViewDelegate>//導(dǎo)入代理
{
CLLocationManager* _locationManager;
}
@property (strong, nonatomic) IBOutlet UITextField *latitudeTF;
@property (strong, nonatomic) IBOutlet UITextField *longitudeTF;
@property (strong, nonatomic) IBOutlet MKMapView *mapV;
@property (strong, nonatomic) CLGeocoder *geocoder;
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
// 需要在Info.plist文件 添加權(quán)限 Privacy - Location When In Use Usage Description 和 Privacy - Location Always Usage Description
// 請求獲取定位授權(quán)
[_locationManager requestAlwaysAuthorization];
// 初始化地理編碼
_geocoder = [[CLGeocoder alloc]init];
// 設(shè)置地圖標(biāo)準(zhǔn)類型
_mapV.mapType = MKMapTypeStandard;
// 縮放
_mapV.zoomEnabled = YES;
// 旋轉(zhuǎn)
_mapV.rotateEnabled = YES;
// 滾動
_mapV.scrollEnabled = YES;
// 交通流量
_mapV.showsTraffic = YES;
// 指南針
_mapV.showsCompass = YES;
// 為MKMapView設(shè)置delegate
_mapV.delegate = self;
// 調(diào)用自己實(shí)現(xiàn)的方法設(shè)置地圖的顯示位置和顯示區(qū)域
[self locateToLatitude:23.126272 longitude:113.395568];
// / 創(chuàng)建一個手勢處理器肮柜,用于檢測椒振、處理長按手勢
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
// 手勢添加到視圖
[self.view addGestureRecognizer:gesture];
}
//查詢按鈕實(shí)現(xiàn)方法
-(IBAction)getBtn:(UIButton *)sender {
// 關(guān)閉兩個文本框的虛擬鍵盤
[_latitudeTF resignFirstResponder];
[_longitudeTF resignFirstResponder];
NSString* latitudeStr = _latitudeTF.text;
NSString* longtitudeStr =_longitudeTF.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];
}
}
//手勢實(shí)現(xiàn)方法
-(void)longPress:(UILongPressGestureRecognizer *)gesture{
// 獲取長按點(diǎn)的坐標(biāo)
CGPoint pos = [gesture locationInView:_mapV];
// 將長按點(diǎn)的坐標(biāo)轉(zhuǎn)換為經(jīng)度值维哈、緯度值
CLLocationCoordinate2D coord = [_mapV convertPoint:pos toCoordinateFromView:_mapV];
//錨點(diǎn)和覆蓋層
#if 1//錨點(diǎn)
// 將經(jīng)度值、緯度值包裝為CLLocation對象
CLLocation *location = [[CLLocation alloc]initWithLatitude:coord.latitude longitude:coord.longitude];
// 根據(jù)經(jīng)度憔维、緯度反向解析地址
[_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks != nil && 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)建MKPointAnnotation對象——代表一個錨點(diǎn)
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
annotation.title = placemark.name;
annotation.subtitle = address;
annotation.coordinate = coord;
//添加到地圖
[_mapV addAnnotation:annotation];
}
}];
#elif 0 //覆蓋層
// 創(chuàng)建MKCircle對象略板,該對象代表覆蓋層
MKCircle* circle = [MKCircle circleWithCenterCoordinate:coord radius:50];
// 添加MKOverlay
[_mapV addOverlay:circle level:MKOverlayLevelAboveLabels];
#endif
}
//自己定義定位方法
-(void)locateToLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{
// 設(shè)置地圖中心的經(jīng)度、緯度
CLLocationCoordinate2D center = {latitude,longitude};
// 設(shè)置地圖顯示的范圍边败,地圖顯示范圍越小袱衷,細(xì)節(jié)越清楚
MKCoordinateSpan span = MKCoordinateSpanMake(0.005,0.005);
// 創(chuàng)建MKCoordinateRegion對象,該對象代表地圖的顯示中心和顯示范圍
MKCoordinateRegion region =MKCoordinateRegionMake(center, span);
// 設(shè)置當(dāng)前地圖的顯示中心和顯示范圍
[_mapV setRegion:region animated:YES];
// 創(chuàng)建MKPointAnnotation對象——代表一個錨點(diǎn)
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
annotation.title = @"主標(biāo)題";
annotation.subtitle = @"詳細(xì)地址";
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(
latitude , longitude);
annotation.coordinate = coordinate;
// 添加錨點(diǎn)
[_mapV addAnnotation:annotation];
}
//渲染覆蓋層代理方法
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
MKCircle * circle = (MKCircle*)overlay;
// 創(chuàng)建一個MKCircleRenderer對象
MKCircleRenderer* render = [[MKCircleRenderer alloc] initWithCircle:circle];
// 設(shè)置MKCircleRenderer的透明度
render.alpha = 0.3;
// 設(shè)置MKCircleRenderer的填充顏色和邊框顏色
render.fillColor = [UIColor blueColor];
render.strokeColor = [UIColor redColor];
return render;
}
//正向解析
NSString *addr = self.addrTF.text;
if (addr != nil && addr.length > 0) {
[geocoder geocodeAddressString:addr completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// 如果解析結(jié)果的集合元素的個數(shù)大于1笑窜,則表明解析得到了經(jīng)度致燥、緯度信息
if (placemarks.count > 0) {
CLPlacemark *placemark = placemarks[0];
CLLocation *location = placemark.location;
self.resultTF.text = [NSString stringWithFormat:@"%@的經(jīng)度為:%g 緯度為:%g",addr,location.coordinate.longitude,location.coordinate.latitude];
}
else
{
NSLog(@"地址無法解析");
}
}];
}
//反向解析
NSString *longitudeStr = _longitudeTF.text;;
NSString *latitudeStr = _latitudeTF.text;
// 判斷不為空
if (latitudeStr != nil && latitudeStr.length > 0 && longitudeStr !=nil && longitudeStr.length >0) {
// 將用戶輸入的經(jīng)度、緯度封裝成CLLocation對象
CLLocation *location = [[CLLocation alloc]initWithLatitude:latitudeStr.floatValue longitude:longitudeStr.floatValue];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// 如果解析結(jié)果的集合元素的個數(shù)大于1排截,則表明解析得到了經(jīng)度嫌蚤、緯度信息
if (placemarks.count > 0) {
// 只處理第一個解析結(jié)果,實(shí)際項(xiàng)目可使用列表讓用戶選擇
CLPlacemark *placemark = placemarks[0];
// 獲取詳細(xì)地址信息
NSArray *addrArray = placemark.addressDictionary[@"FormattedAddressLines"];
// 將詳細(xì)地址拼接成一個字符串
NSMutableString *addr = [[NSMutableString alloc]init];
for (int i = 0; i < addrArray.count; i++) {
[addr appendString:addrArray[i]];
}
_resultTF.text = [NSString stringWithFormat:@"經(jīng)度為:%g,緯度為:%g的地址為%@",location.coordinate.longitude,location.coordinate.latitude,addr];
}
else
{
NSLog(@"您輸入的緯度無法解析");
}
}];
}