请选择 进入手机版 | 继续访问电脑版
//
// kuaicheViewController.m


//


#import "kuaicheViewController.h"
//导入系统类
#import <MapKit/MapKit.h>
//导入获取经纬度类
#import <CoreLocation/CoreLocation.h>
@interface kuaicheViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>
//创建地图对象
@property(nonatomic,strong)MKMapView *MapView;
@property(nonatomic,strong)CLLocationManager *lomanger;




@end


@implementation kuaicheViewController


- (void)viewDidLoad {
[super viewDidLoad];
//实例化
self.MapView = [[MKMapView alloc] initWithFrame:self.view.frame];
//设置代理
self.MapView.delegate = self;
//设置地图样式
self.MapView.mapType = MKMapTypeStandard;
//加载视图
[self.view addSubview:self.MapView];
//设置按钮
UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
//设置
butt.frame = CGRectMake(350, 430,50, 50);
//添加文字
[butt setTitle:@"当前位置" forState:UIControlStateNormal];
butt.titleLabel.font = [UIFont systemFontOfSize:10];
//添加点击事件
[butt addTarget:self action:@selector(dian:) forControlEvents:UIControlEventTouchUpInside];
butt.layer.masksToBounds = YES;
butt.layer.cornerRadius = 25;
butt.backgroundColor = [UIColor greenColor];
//添加视图
[self.MapView addSubview:butt];
UIView *vi = [[UIView alloc]initWithFrame:CGRectMake(10, 500, self.view.frame.size.width-20, 200)];
vi.backgroundColor = [UIColor whiteColor];
UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-20, 200)];
imgV.image = [UIImage imageNamed:@"66776"];
[vi addSubview:imgV];


// UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 20, 60, 30)];
// [btn setTitle:@"现在" forState:UIControlStateNormal];
// [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// btn.layer.masksToBounds = YES;
// btn.layer.cornerRadius = 10;
// btn.layer.borderWidth = 1;
// btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
//
// [vi addSubview:btn];
[self.view addSubview:vi];
//实例化经纬度类
self.lomanger = [[CLLocationManager alloc] init];
//申请用户授权用户进入后台不在授权
[self.lomanger requestWhenInUseAuthorization];
}
- (void)YOU:(id)seb{
//
CLGeocoder *g = [[CLGeocoder alloc] init];
//将地址字符串转换为位置的经纬度
[g geocodeAddressString:self.provie completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//获取位置随便展示在地图上
CLPlacemark *place = [placemarks lastObject];
//获取位置
CLLocation *loc =place.location;
CLLocationCoordinate2D coor = loc.coordinate;
//定位
MKPointAnnotation *anne = [[MKPointAnnotation alloc] init];
//设置挫钉
anne.coordinate = coor;
//回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
//设置让地图显示区域缩小
MKCoordinateRegion rgin =MKCoordinateRegionMakeWithDistance(coor, 10, 10);
//添加到视图
[self.MapView setRegion:rgin];
[self.MapView addAnnotation:anne];
});
}];
}
//实现点击方法
- (void)dian:(id)sender{
//设置代理
self.lomanger.delegate = self;
//开始获取位置信息 调用此方法后协议中的方法才会执行
[self.lomanger startUpdatingLocation];
}
//实现代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//创建对象获取当前获取最后一个元素
CLLocation *curloc = [locations lastObject];
//创建结构体 获取经纬度
CLLocationCoordinate2D curCoor = curloc.coordinate;
self.lomanger.delegate = nil;
//输出经纬度
NSLog(@"经度%g,纬度%g",curCoor.longitude,curCoor.latitude);
//设置让地图显示区域缩小
MKCoordinateRegion rgin =MKCoordinateRegionMakeWithDistance(curCoor, 30, 30);
//设置动画并添加
[self.MapView setRegion:rgin animated:YES];
//将地址经纬度转换为字符串
CLGeocoder *Geder = [[CLGeocoder alloc] init];
//设置方法
[Geder reverseGeocodeLocation:curloc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//创建异步队列回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
//获取最后一个经纬度转换为字符串
CLPlacemark *place = [placemarks firstObject];
//设置大头针
MKPointAnnotation *pino = [[MKPointAnnotation alloc] init];
//将获取的地址名字给大头针
pino.title = place.name;
//设置大头针的位置
pino.coordinate = curCoor;
//添加到地图上
[self.MapView addAnnotation:pino];
});
}];
}
//实现大头针点击事件
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
//从MAPView找一块可用的内存
MKPinAnnotationView *kl =[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"1"];
//设置动画
kl.animatesDrop = YES;
//设置
kl.canShowCallout = YES;
//返回内容
return kl;
}


/*
#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/


@end
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|翁笔

Powered by Discuz! X3.3 © 2001-2018 Comsenz Inc.

返回顶部