申明:此为本人学习笔记,若有纰漏错误之处的可留言共同探讨
/*
map点击获取当前地址的具体坐标 思路
创建mapView,创建手势,接着把手势获取到的点交给mapView,即可得出需要的内容。
map点击获取当前地址的具体坐标 步骤
1 导入库和头文件
2 创建全局变量MkMapView并初始化
3 创建手势
4 通过手势方法locationInView获取点位置,用CGPoint保存
5 把获得的点赋给(convertPoint)地图 转成经纬度CLLocationCoordinate2D保存
6 获取经纬度
7 输出(或者做其他想做的事情)
*/
#import "ViewController.h"
#define SCREEN_WIDTH self.view.frame.size.width
#define SCREEN_HEIGHT self.view.frame.size.height
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 地图页面
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 20,SCREEN_WIDTH, SCREEN_HEIGHT-20)];
[self.view addSubview:mapView];
// 手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(getPoint:)];
[mapView addGestureRecognizer:tap];
}
#pragma mark - 点击方法
- (void)getPoint:(UITapGestureRecognizer *)tap
{
CGPoint point = [tap locationInView:mapView];
CLLocationCoordinate2D coordinate = [mapView convertPoint:point toCoordinateFromView:mapView];
NSLog(@"纬度%f -- 经度%f",coordinate.latitude,coordinate.longitude);
CLLocation *loaction = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:loaction completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *mark = [placemarks firstObject];
NSLog(@"%@",mark.name);
NSLog(@"%@",mark.administrativeArea);
NSLog(@"%@",mark.locality);
NSLog(@"%@",mark.subLocality);
NSLog(@"%@",mark.thoroughfare);
}];
}