IOS通过长按手势放置一个大头针---by talent.L

#import "ViewController.h"

#import

@interface ViewController ()

{  //地图

MKMapView *mapView;

}

//地理管理器

@property (nonatomic,strong)CLLocationManager * manage;

@end

@implementation ViewController

//地理管理器

-(CLLocationManager *)manage{

if (_manage==nil) {

_manage =[[CLLocationManager alloc]init];

_manage.delegate =self;

//多远更新一次

_manage.distanceFilter =100;

//设置误差

_manage.desiredAccuracy = kCLLocationAccuracyBest;

//设置地图在条件允许下的情况自动停止定位(省电模式)

_manage.pausesLocationUpdatesAutomatically =YES;

}

return _manage;

}

- (void)viewDidLoad {

[super viewDidLoad];

//导入三个文件

//1.corelocation      2.coregraphics        3.MapKit

//  info.plist改   Privacy - Location Always Usage Description

//授权可以定位 总是允许

[self.manage requestAlwaysAuthorization];

mapView =[[MKMapView alloc]initWithFrame:self.view.bounds];

//设置类型

mapView.mapType =MKMapTypeStandard;

//可旋转

mapView.rotateEnabled =YES;

//可倾斜

mapView.pitchEnabled =YES;

//可捏合

mapView.zoomEnabled =YES;

//可滚动

mapView.scrollEnabled=YES;

//可见自己位置

mapView.showsUserLocation =YES;

//设置代理

mapView.delegate =self;

//跟踪用户方向

mapView.userTrackingMode =MKUserTrackingModeFollowWithHeading;

//添加父视图

[self.view addSubview:mapView];

//设置长按手势

UILongPressGestureRecognizer * longpress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];

[mapView addGestureRecognizer:longpress];

}

-(void)click:(UILongPressGestureRecognizer *)reco

{

[self.manage startUpdatingLocation];

// 获得手势点击的坐标

CGPoint point = [reco locationInView:self.myMapView];

// 把坐标点转换为经纬度信息

CLLocationCoordinate2D coor = [self.myMapView convertPoint:point toCoordinateFromView:self.myMapView];

NSLog(@"纬度:%g,经度:%g",coor.latitude,coor.longitude);

// 添加锚点

MKPointAnnotation *pointAnno = [[MKPointAnnotation alloc] init];

pointAnno.coordinate = coor;

pointAnno.title = [NSString stringWithFormat:@"纬度:%g,经度:%g",coor.latitude,coor.longitude];

// 根据经纬度解析为地址字符串()

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];

});

}];

}

//代理方法 更新位置自动回调- (void)locationManager:(CLLocationManager *)manager    didUpdateLocations:(NSArray*)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0){

//获得地理中最后一个地理数据位置的经纬度

CLLocationCoordinate2D coor = [locations lastObject].coordinate;

//设置显示范围精度

MKCoordinateSpan span;

span.latitudeDelta = 0.01;

span.longitudeDelta = 0.01;

//设置地区

MKCoordinateRegion  region = {coor,span};

//设置该位置为地图可显示位置

[mapView regionThatFits:region];

[mapView setRegion:region animated:YES];

}


//如果拖拽 往设置的经纬度上放置大头针

- (IBAction)aa:(id)sender {

[self.manage startUpdatingLocation];

// 把坐标点转换为经纬度信息

CLLocationCoordinate2D coor = {[self.mytext1.text floatValue],[self.mytext2.text floatValue]};

MKCoordinateSpan span;

span.latitudeDelta = 0.01;

span.longitudeDelta = 0.01;

MKCoordinateRegion region = {coor,span};

// 添加锚点

MKPointAnnotation *pointAnno = [[MKPointAnnotation alloc] init];

pointAnno.coordinate = coor;

pointAnno.title = [NSString stringWithFormat:@"纬度:%g,经度:%g",coor.latitude,coor.longitude];

// 根据经纬度解析为地址字符串()

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 {   

//授权可以定位 总是允许  

[self.manage requestAlwaysAuthorization];        

[self.geocoder geocodeAddressString:_mytext.text completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {    

  // 只处理第一个解析结果,实际项目中可使用列表让用户选择   

    CLPlacemark *place = placemarks[0];    

  // 获得这个地址的位置信息   

    CLLocation *location = place.location;  

    //  获得经纬度信息   

    CLLocationCoordinate2D coor = location.coordinate;  

    self.mytextView.text =[NSString stringWithFormat:@"%f,%f",coor.latitude,coor.longitude];            }];    }

//反解析- (IBAction)aaa:(id)sender {  

//授权可以定位 总是允许

  [self.manage requestAlwaysAuthorization];   

//将长按点的坐标转换为经度、纬度值    

  CLLocationCoordinate2D coord = {[self.mytextwei.text floatValue],[self.mytextjing.text floatValue]};      

//将经度纬度值包装为CLLocation对象    

  CLLocation *location=[[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];     

  //根据经纬度反向解析地址  

    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {

//获取解析得到的第一个地址信息

CLPlacemark *placemark =[placemarks lastObject];

//获取地址信息中的FromattedAddressLines对应的详细地址

//

//        NSArray *addArray=placemark.addressDictionary[@"formattedAddressLines"];

//

//        //将详细地址拼接成一个字符串

//

//        NSMutableString *address=[[NSMutableString alloc] init];

//

//        for (int i = 0; i < addArray.count ; i ++) {

//

//            [address appendString:addArray[i]];

//

//        }

//        NSLog(@"11====================%@",address);

//    副标题  annotation.subtitle = address;

self.mytextView.text = [NSString stringWithFormat:@"%@%@%@",placemark.country,placemark.administrativeArea,placemark.name];

}] ;

}

你可能感兴趣的:(IOS通过长按手势放置一个大头针---by talent.L)