iOS 地图

CoreLocation的简单使用:

在地图之前,首先会讲到以个很重要的框架<CoreLocation>,顾名思义,就是用来定位的框架。

  1. 获取用户当前的位置:
    - (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //创建位置管理对象
    _locationManager = [[CLLocationManager alloc]init];
    //加载到位置就会调用代理方法
    _locationManager.delegate = self;
    }
    //加载到位置之后的回调
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
    if (!locations.count) return;
    //在加载到得绘制当中取出最近获取的位置
    CLLocation *currentLocation = [locations lastObject];
    //如果仅仅获取下位置,在拿到位置之后就可以停止更新位置了,毕竟定位耗电。
    [manager stopUpdatingLocation];
    }
    • 注意,iOS8之后获取用户位置之前要配置好info.plist并且要申请用户授权
      • 添加字段:NSLocationWhenInUseDescription或者NSLocationAlwaysUsageDescription

      • 请求用户的授权,要什么类别的授权,前面就添加什么样的字段

         //只有用户是未决定状态才需要申请授权。
         //如果用户已经拒绝授权,这里请求也不会弹框
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
           [self.manager requestAlwaysAuthorization];
          }
        
  2. 地理编码及反地理编码:
  • 地理编码:根据字符串类型的地名,转换成CLPlaceMark对象:
    geocoder = [[CLGeocoder alloc]init];
    [self.geocoder geocodeAddressString:@"武汉" completionHandler:^(NSArray *placemarks, NSError *error) {
    }];
  • 反地理编码:根据地理位置,转换成CLPlaceMark对象:
    geocoder = [[CLGeocoder alloc]init];
    [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
    }];

MapKit的使用

地图中最核心的内容当然是<MapKit>了,里面已经包含了<CoreLocation>框架。最关键的是里面提供了一个叫MKMapView的控件,用来展示地图。

  1. 展示地图,导入库

    • 如果不导入库,可能会在storyboard上使用MKMapView时报错。
  2. 如何展示

    • iOS展示地图只需要将MapView用上就可以。
    • 其他的东西用到是再查一下。
  3. 添加大头针

    • 调用MapView的addAnnotation方法即可添加大头针模型;

    • 这里同样是MVC模式,我们只需要添加数据就好,苹果会根据我们传入的数据,展示UI

    • 通过代理方法返回展示的大头针视图,如果返回空,就会使用系统默认的大头针视图。

      -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
      //系统会自己调用annoView的setAnnotation:方法,来个大头针视图设置模型数据。所以这里可以省略传递模型的一部。(但是要在annoView的setAnnotation:方法中做好相应的处理)
      return annoView;
      }
      
  4. 利用系统导航

    • 记得使用的是[MKMapItem openMapsWithItems:@[sourceItem,destinationItem] launchOptions:options];方法就行
  5. 画线

    MKDirections *directions = [[MKDirections alloc]initWithRequest:dirRequest];
     [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
         if (error || !response.routes.count) {
             return;
         }
         for (MKRoute * route in response.routes) {
             [self.mapView addOverlay:route.polyline];
         }
     }];
     //返回画好的线
     - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
     MKPolylineRenderer *lineRenderer = [[MKPolylineRenderer alloc]initWithPolyline:overlay];
     lineRenderer.lineWidth = 5.0;
     lineRenderer.strokeColor = [UIColor redColor];
     return lineRenderer;
      } 
    

百度地图的使用

相比系统的地图,百度地图的功能更加亲民、使用。比如有poi搜索、公交线路搜索等等。

  1. 框架的导入
    • 看报什么错,找到对应的库,添加就好。或者直接对着文档,一次性把所有要添加的库都加上
  2. 基本的使用
    • 看着文档来,没什么难度。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容