第一步 : 设置定位服务请求 在plist文件中添加下面配置中至少一个:
1)NSLocationAlwaysAndWhenInUseUsageDescription
2)NSLocationWhenInUseUsageDescription
如果Xcode9.0以上版本请设置网络请求
第二步 :导入头文件
#import
#import
第三步 : 设置代理和定义全局变量
@interface ViewController ()
{
MKMapView* _mapView;//地图视图
CLLocationManager*_locationManager;//定位管理类
@end
第四步:实现代码
- (void)viewDidLoad {
[super viewDidLoad];
/**创建地图*/
[self creatMap];
/**定位当前*/ // 模拟器上无法定位
[self selfLocation];
}
#pragma mark定位自己
-(void)selfLocation{
//创建定位对象
_locationManager =[[CLLocationManager alloc] init];
//设置定位属性
_locationManager.desiredAccuracy =kCLLocationAccuracyBest;
//设置定位更新距离militer
_locationManager.distanceFilter= 10.0;
//绑定定位委托
_locationManager.delegate = self;
/**设置用户请求服务*/
//1.去info.plist文件添加定位服务描述,设置的内容可以在显示在定位服务弹出的提示框 //取出当前应用的定位服务状态(枚举值)
CLAuthorizationStatus status =[CLLocationManager authorizationStatus];
//如果未授权则请求
if(status==kCLAuthorizationStatusNotDetermined) {
[_locationManager requestAlwaysAuthorization];
}
//开始定位
[_locationManager startUpdatingLocation];
}
#pragma mark CLLocationManagerDelegate
//定位后的返回结果
-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations{
//locations数组中存储的是CLLocation
//遍历数组取出坐标--》如果不需要也可以不遍历
CLLocation*location =[locations firstObject];
//火星坐标转地球坐标
//location=[location locationMarsFromEarth];
//设置地图显示该经纬度的位置
MKCoordinateRegion region =MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.01,0.01));
[_mapView setRegion:region animated:YES];
//创建大头针
MKPointAnnotation * pointAnnotation = [[MKPointAnnotation alloc] init];
//设置经纬度
pointAnnotation.coordinate = location.coordinate;
//设置主标题
pointAnnotation.title =@"The location of the I";
//设置副标题
pointAnnotation.subtitle =@"Hello Map";
//将大头针添加到地图上
[_mapView addAnnotation:pointAnnotation];
}
#pragma mark创建地图
-(void)creatMap{
//实例化
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
// 是否可以缩放
_mapView.zoomEnabled = YES;
// 是否可以滚动
_mapView.scrollEnabled = YES;
// 是否可以旋转
_mapView.rotateEnabled = YES;
// 是否显示3D
_mapView.pitchEnabled = YES;
// 是否显示指南针
_mapView.showsCompass = YES;
// 是否显示比例尺
_mapView.showsScale = YES;
// 是否显示交通
_mapView.showsTraffic = YES;
// 是否显示建筑物
_mapView.showsBuildings = YES;
//绑定委托
_mapView.delegate=self;
//添加到界面
[self.view addSubview:_mapView];
//添加手势
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
//添加到地图上
[_mapView addGestureRecognizer:longPress];
//创建UISegmentedControl
NSArray*mapTypeArray =@[@"标准",@"卫星地图",@"混合"];
UISegmentedControl *segment =[[UISegmentedControl alloc] initWithItems:mapTypeArray];
segment.frame=CGRectMake(50,50,300,50);
[_mapView addSubview:segment];
segment.selectedSegmentIndex=0;
//添加UISegmentedControl的事件响应方法
[segment addTarget:self action:@selector(mapTypeChanged:) forControlEvents:UIControlEventValueChanged];
}
//手势方法
-(void)longPress:(UILongPressGestureRecognizer *)sender{
//判断是否是长按
if(sender.state!=UIGestureRecognizerStateBegan) {
return;
}
//获取手势在uiview上的位置
CGPoint longPressPoint = [sender locationInView:_mapView];
//将手势在uiview上的位置转化为经纬度
CLLocationCoordinate2D coordinate2d =[_mapView convertPoint:longPressPoint toCoordinateFromView:_mapView];
NSLog(@"%f%f",coordinate2d.longitude,coordinate2d.latitude);
//添加大头针
MKPointAnnotation *pointAnnotation =[[MKPointAnnotation alloc] init];
//设置经纬度
pointAnnotation.coordinate= coordinate2d;
//设置主标题和副标题
pointAnnotation.title=[NSString stringWithFormat:@"纬度:%g,经度:%g",coordinate2d.latitude,coordinate2d.longitude];
pointAnnotation.subtitle=@"你好,世界!";
//添加到地图上
[_mapView addAnnotation:pointAnnotation];
MKCircle*circle =[MKCircle circleWithCenterCoordinate:coordinate2d radius:50]; //先添加,在回调方法中创建覆盖物
[_mapView addOverlay:circle];
}
//大头针的回调方法(与cell的复用机制很相似)
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{
// ------- 自带的大头针-----
// //复用
// MKPinAnnotationView *annotationView =(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN"];
// //判断复用池中是否有可用的
// if(annotationView==nil) {
// annotationView =(MKPinAnnotationView *)[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN"];
// }
// 添加左边的视图
// UIImageView *imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow"]];
// imageView.frame=CGRectMake(0,0,50,50);
// annotationView.leftCalloutAccessoryView=imageView;
// //显示
// annotationView.canShowCallout=YES;
// //设置是否显示动画
// annotationView.animatesDrop=YES;
// //设置右边视图
// UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(0,0,30,30)];
// label.text=@">>";
// annotationView.rightCalloutAccessoryView=label;
// //设置大头针的颜色
// annotationView.pinColor=MKPinAnnotationColorPurple;
// return annotationView;
/** 自定义大头针-------*/
static NSString *pinId = @"pinID";
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:pinId];
if (annoView == nil) {
annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinId];
}
annoView.annotation = annotation;
annoView.image = [UIImage imageNamed:@"2.jpg"];
annoView.canShowCallout = YES;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.jpg"]];
imageView.bounds = CGRectMake(0, 0, 44, 44);
annoView.leftCalloutAccessoryView = imageView; imageView.userInteractionEnabled = YES;
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.jpg"]];
imageView2.bounds = CGRectMake(0, 0, 44, 44);
annoView.rightCalloutAccessoryView = imageView2;
annoView.draggable = YES;
return annoView;
}
//覆盖物的回调方法
-(MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id)overlay{
//创建圆形覆盖物
MKCircleRenderer*circleRender =[[MKCircleRenderer alloc]initWithCircle:overlay]; //设置填充色
circleRender.fillColor=[UIColor purpleColor];
//设置边缘颜色
circleRender.strokeColor=[UIColor redColor];
return circleRender;
}
//解决手势冲突,可以同时使用多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{
return YES;
}
//segment响应回调
-(void)mapTypeChanged:(UISegmentedControl*)sender{
//获得当前segment索引
NSInteger index = sender.selectedSegmentIndex;
//设置地图的显示方式
_mapView.mapType= index;
}
作者:Sampson_5f51
链接:https://www.jianshu.com/p/23b8a7b44230
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
网友评论