地图是ios开发中常用到的功能,本文介绍苹果原生api地图的使用方法,使用地图必定要进行网络连接,注意的是苹果在ios9以后引入了https,所以进行网络请求时可以使用下面方法:
第一步:在Info.plist中添加NSAppTransportSecurity类型Dictionary。 第二步:在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES 接下来要做的是设置定位服务请求,在plist文件中天下下面配置中至少一个:
1)NSLocationAlwaysUsageDescription
2)NSLocationWhenInUseUsageDescription
上面两步完成了以后,我们就可以进行地图的开发了:
实现定位要导入地图头文件,本例中使用原生地图,所以导入<Map.Kit/MapKit.h>后就可以创建地图了,如要获取指定经纬度或者实时定位自己的位置则还要导入核心定位服务头文件<CoreLocation/CoreLocation.h>,在这里我做了一个简单的Demo用以实现定位,值得一提的是苹果使用的是高德地图,其定位经纬度与地图定位度虐有偏差(具体原因你懂得),如果要实现准备定位,还要进行火星坐标(天朝)转地球坐标的运算(这个有人已经帮我们做好了,需要的可以上百度上去下),以下是代码的内容:
#import @interfaceMapViewController : UIViewController @end //MapViewController.m //MapTest // //Created by on 16/5/15. //Copyright © 2016年mobiletrain. All rights reserved. // #import"MapViewController.h" /**核心定位服务头文件*/ #import <CoreLocation/CoreLocation.h> /**原生地图头文件*/ #import<MapKit/MapKit.h> /**火星坐标转换头文件*/ //#import"CLLocation+Sino.h" @interfaceMapViewController () @end @implementationMapViewController{ MKMapView * _mapView;//地图视图 CLLocationManager *_locationManager;//定位管理类 } - (void)viewDidLoad { [superviewDidLoad]; /**创建地图*/ [self creatMap]; /**定位当前*/ [selfselfLocation]; } #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 =@"我在这里"; //设置副标题 pointAnnotation.subtitle =@"hello world"; //将大头针添加到地图上 [_mapView addAnnotation:pointAnnotation]; } #pragma mark创建地图 -(void)creatMap{ //实例化 _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; //绑定委托 _mapView.delegate=self; //添加到界面 [self.view addSubview:_mapView]; //添加手势 UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@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:selfaction:@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=@"我在这里"; 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; returnannotationView; } //覆盖物的回调方法 -(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay{ //创建圆形覆盖物 MKCircleRenderer *circleRender =[[MKCircleRenderer alloc] initWithCircle:overlay]; //设置填充色 circleRender.fillColor=[UIColor purpleColor]; //设置边缘颜色 circleRender.strokeColor=[UIColor redColor]; returncircleRender; } //解决手势冲突,可以同时使用多个手势 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { returnYES; } //segment响应回调 -(void)mapTypeChanged:(UISegmentedControl *)sender{ //获得当前segment索引 NSInteger index =sender.selectedSegmentIndex; //设置地图的显示方式 _mapView.mapType =index; } @end 源码地址