#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
{? //地圖
MKMapView *mapView;
}
//地理管理器
@property (nonatomic,strong)CLLocationManager * manage;
@end
@implementation ViewController
//地理管理器
-(CLLocationManager *)manage{
if (_manage==nil) {
_manage =[[CLLocationManager alloc]init];
_manage.delegate =self;
//多遠(yuǎn)更新一次
_manage.distanceFilter =100;
//設(shè)置誤差
_manage.desiredAccuracy = kCLLocationAccuracyBest;
//設(shè)置地圖在條件允許下的情況自動停止定位(省電模式)
_manage.pausesLocationUpdatesAutomatically =YES;
}
return _manage;
}
- (void)viewDidLoad {
[super viewDidLoad];
//導(dǎo)入三個文件
//1.corelocation? ? ? 2.coregraphics? ? ? ? 3.MapKit
//? info.plist改 ? Privacy - Location Always Usage Description
//授權(quán)可以定位 總是允許
[self.manage requestAlwaysAuthorization];
mapView =[[MKMapView alloc]initWithFrame:self.view.bounds];
//設(shè)置類型
mapView.mapType =MKMapTypeStandard;
//可旋轉(zhuǎn)
mapView.rotateEnabled =YES;
//可傾斜
mapView.pitchEnabled =YES;
//可捏合
mapView.zoomEnabled =YES;
//可滾動
mapView.scrollEnabled=YES;
//可見自己位置
mapView.showsUserLocation =YES;
//設(shè)置代理
mapView.delegate =self;
//跟蹤用戶方向
mapView.userTrackingMode =MKUserTrackingModeFollowWithHeading;
//添加父視圖
[self.view addSubview:mapView];
//設(shè)置長按手勢
UILongPressGestureRecognizer * longpress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
[mapView addGestureRecognizer:longpress];
}
-(void)click:(UILongPressGestureRecognizer *)reco
{
[self.manage startUpdatingLocation];
// 獲得手勢點(diǎn)擊的坐標(biāo)
CGPoint point = [reco locationInView:self.myMapView];
// 把坐標(biāo)點(diǎn)轉(zhuǎn)換為經(jīng)緯度信息
CLLocationCoordinate2D coor = [self.myMapView convertPoint:point toCoordinateFromView:self.myMapView];
NSLog(@"緯度:%g,經(jīng)度:%g",coor.latitude,coor.longitude);
// 添加錨點(diǎn)
MKPointAnnotation *pointAnno = [[MKPointAnnotation alloc] init];
pointAnno.coordinate = coor;
pointAnno.title = [NSString stringWithFormat:@"緯度:%g,經(jīng)度:%g",coor.latitude,coor.longitude];
// 根據(jù)經(jīng)緯度解析為地址字符串()
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:coor.latitude longitude:coor.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMark = [placemarks lastObject];
pointAnno.subtitle = placeMark.name;
dispatch_async(dispatch_get_main_queue(), ^{
[self.myMapView addAnnotation:pointAnno];
});
}];
}
//代理方法 更新位置自動回調(diào)- (void)locationManager:(CLLocationManager *)manager? ? didUpdateLocations:(NSArray*)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0){
//獲得地理中最后一個地理數(shù)據(jù)位置的經(jīng)緯度
CLLocationCoordinate2D coor = [locations lastObject].coordinate;
//設(shè)置顯示范圍精度
MKCoordinateSpan span;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
//設(shè)置地區(qū)
MKCoordinateRegion? region = {coor,span};
//設(shè)置該位置為地圖可顯示位置
[mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
//如果拖拽 往設(shè)置的經(jīng)緯度上放置大頭針
- (IBAction)aa:(id)sender {
[self.manage startUpdatingLocation];
// 把坐標(biāo)點(diǎn)轉(zhuǎn)換為經(jīng)緯度信息
CLLocationCoordinate2D coor = {[self.mytext1.text floatValue],[self.mytext2.text floatValue]};
MKCoordinateSpan span;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
MKCoordinateRegion region = {coor,span};
// 添加錨點(diǎn)
MKPointAnnotation *pointAnno = [[MKPointAnnotation alloc] init];
pointAnno.coordinate = coor;
pointAnno.title = [NSString stringWithFormat:@"緯度:%g,經(jīng)度:%g",coor.latitude,coor.longitude];
// 根據(jù)經(jīng)緯度解析為地址字符串()
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:coor.latitude longitude:coor.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMark = [placemarks lastObject];
pointAnno.subtitle = placeMark.name;
dispatch_async(dispatch_get_main_queue(), ^{
[self.mymapView addAnnotation:pointAnno];
});
}];
[self.mymapView regionThatFits:region];
[self.mymapView setRegion:region];
}
//解析反解析
//解析- (IBAction)a:(id)sender {? ?
//授權(quán)可以定位 總是允許??
[self.manage requestAlwaysAuthorization];? ? ? ??
[self.geocoder geocodeAddressString:_mytext.text completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {? ??
? // 只處理第一個解析結(jié)果炭懊,實(shí)際項(xiàng)目中可使用列表讓用戶選擇? ?
? ? CLPlacemark *place = placemarks[0];? ??
? // 獲得這個地址的位置信息? ?
? ? CLLocation *location = place.location;??
? ? //? 獲得經(jīng)緯度信息? ?
? ? CLLocationCoordinate2D coor = location.coordinate;??
? ? self.mytextView.text =[NSString stringWithFormat:@"%f,%f",coor.latitude,coor.longitude];? ? ? ? ? ? }];? ? }
//反解析- (IBAction)aaa:(id)sender {??
//授權(quán)可以定位 總是允許
? [self.manage requestAlwaysAuthorization];? ?
//將長按點(diǎn)的坐標(biāo)轉(zhuǎn)換為經(jīng)度僻造、緯度值? ??
? CLLocationCoordinate2D coord = {[self.mytextwei.text floatValue],[self.mytextjing.text floatValue]};? ? ??
//將經(jīng)度緯度值包裝為CLLocation對象? ??
? CLLocation *location=[[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];? ? ?
? //根據(jù)經(jīng)緯度反向解析地址??
? ? [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {
//獲取解析得到的第一個地址信息
CLPlacemark *placemark =[placemarks lastObject];
//獲取地址信息中的FromattedAddressLines對應(yīng)的詳細(xì)地址
//
//? ? ? ? NSArray *addArray=placemark.addressDictionary[@"formattedAddressLines"];
//
//? ? ? ? //將詳細(xì)地址拼接成一個字符串
//
//? ? ? ? NSMutableString *address=[[NSMutableString alloc] init];
//
//? ? ? ? for (int i = 0; i < addArray.count ; i ++) {
//
//? ? ? ? ? ? [address appendString:addArray[i]];
//
//? ? ? ? }
//? ? ? ? NSLog(@"11====================%@",address);
//? ? 副標(biāo)題? annotation.subtitle = address;
self.mytextView.text = [NSString stringWithFormat:@"%@%@%@",placemark.country,placemark.administrativeArea,placemark.name];
}] ;
}