MapKit

作者: linatan | 来源:发表于2016-08-17 00:30 被阅读270次

    Location and Maps Programming Guide中提及到主要由Core Location框架提供定位和根据设备方向指引;Map Kit框架提供地图展示,标注等。

    Location-based information consists of two pieces: location services and maps. Location services are provided by the Core Location framework, which defines Objective-C interfaces for obtaining information about the user’s location and heading (the direction in which a device is pointing). Maps are provided by the Map Kit framework, which supports both the display and annotation of maps similar to those found in the Maps app. (To use the features of the Map Kit framework, you must turn on the Maps capability in your Xcode project.) Location services and maps are available on both iOS and OS X.

    引用一张图片来自API Reference 系列 之Mapkit解析

    地图相关类

    Demo实现

    定位用户位置

    请求授权

    1. ios10中提出涉及用户隐私的必须在Info.plist 里声明 XXXUsageDescription,来获取保护的数据。
    plist description
    1. 调用[_locationManager requestWhenInUseAuthorization];或者[_locationManager requestAlwaysAuthorization];

    获取用户当前的坐标

    1. 定义一个mapView
    2. 设置其属性showsUserLocation为YES
    3. 实现MKMapViewDelegatemapView:didUpdateUserLocation:mapView:didFailToLocateUserWithError:

    具体的代码如下:

    //初始化mapView
        _mapView = [[MKMapView alloc]init];
        _mapView.frame = self.view.frame;
        _mapView.delegate = self;
        //显示用户当前位置
         _mapView.showsUserLocation = YES;
        //地图显示类型
        _mapView.mapType = MKMapTypeStandard;
        //显示标尺
        _mapView.showsScale = YES;
        //显示交通状态
        _mapView.showsTraffic = YES;
        //显示罗盘
        _mapView.showsCompass = YES;
        //跟踪用户位置
        _mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
        
        self.view = _mapView;
        //定位授权
        [_locationManager requestWhenInUseAuthorization];
    
    //实现MKMapViewDelegate定位用户位置相关的方法
    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
        static dispatch_once_t centerMapFirstTime;
        
        if (userLocation.coordinate.latitude != 0 && userLocation.coordinate.longitude != 0)
        {
            dispatch_once(&centerMapFirstTime, ^{
                //定位到用户所在位置
                _mapView.centerCoordinate = userLocation.coordinate;
                //地图显示多大区域,MKCoordinateSpanMake中数值越小显示的越精细
                [_mapView setRegion:MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.07,0.07)) animated:YES];
            });
        }
        
        [_geocoder reverseGeocodeLocation:_mapView.userLocation.location completionHandler:^(NSArray * placemarks, NSError * _Nullable error) {
            if (placemarks && placemarks.count > 0)
            {
                //通过经纬度反编码获取到该位置的具体信息,哪个地区,哪条街道等
                _placeMark = placemarks[0];
                //....
            }
        }];
    }
    
    
    - (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
    {
        NSString * message;
        if (error.code == kCLErrorLocationUnknown)
        {
            message = @"无法定位您的位置";
        }
        else
        {
            message = error.description;
        }
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"好的"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * _Nonnull action) {}]];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    

    当前位置区域显示

    用户当前位置

    Annotation

    地图上类似大头针,其data与View分离,data需要是实现MKAnnotation protocol的类,view需要继承于MKAnnotationView

    通用过程是

    1. 定义一个MKPointAnnotation对象,设置其经纬度坐标,标题,副标题
    2. 调用 MKMapViewaddAnnotation: 方法将pointAnnotation添加到地图
    3. 实现MKMapViewDelegate 代理方法
      mapView:viewForAnnotation:
    4. 在代理内定义MKAnnotationView或者MKPinAnnotationView或者其他自定义AnnotationView对象

    代码如下:

    //初始话_pointAnnotation
        CLLocationCoordinate2D coordinate1;
        coordinate1.latitude = 30.289845;
        coordinate1.longitude = 120.186883;
        _pointAnnotation = [[MKPointAnnotation alloc]init];
        _pointAnnotation.coordinate = coordinate1;
        _pointAnnotation.title = @"奶茶 刘若英";
        _pointAnnotation.subtitle = @"原来你也在这里 盛大开演";
        [_mapView addAnnotation:_pointAnnotation];
    
    - (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        if ([annotation isKindOfClass:[MKPointAnnotation class]])
        {
            //这边和tableView中cell重用机制相似
            NSString *pointAnnotationIdentifier =@"pointAnnotationIdentifier";
            MKAnnotationView *annotationView = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:pointAnnotationIdentifier];
            if (!annotationView)
            {
                annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:_pointAnnotation reuseIdentifier:pointAnnotationIdentifier];
            }
            annotationView.annotation = _pointAnnotation;
            //是否显示方框内容
            annotationView.canShowCallout = YES;
            annotationView.draggable = YES;
            return annotationView;
        }
        return nil;
    }
    

    补全用户当前位置的annotation

    之前用户当前位置显示的信息过于简单,希望显示出具体的位置信息,可以在mapView:didUpdateUserLocation:方法中增加

        [_geocoder reverseGeocodeLocation:_mapView.userLocation.location completionHandler:^(NSArray * placemarks, NSError * _Nullable error) {
            if (placemarks && placemarks.count > 0)
            {
                _placeMark = placemarks[0];
                userLocation.title = @"当前位置";
                userLocation.subtitle = [NSString stringWithFormat:@"%@ %@",_placeMark.locality?_placeMark.locality:@"",_placeMark.thoroughfare?_placeMark.thoroughfare:@""];
            }
        }];
    

    用户当前位置的上方框中信息就会显示具体街道,之后demo中会有演示

    自定义大头针显示

    MKAnnotationView可以自定义视图,根据rightCalloutAccessoryView、leftCalloutAccessoryView、detailCalloutAccessoryView改变各个区块的样式,同时如果自己设置view继承MKAnnotationView,可以不受限于left,right,可以自定义方框中展示的样子。

    如果要改变大头针的样式,可以改变MKAnnotationViewimage属性

    annotationView.image = [UIImage imageNamed:@"icon"];
    

    代码示例:

    @interface ArtistAnnotation : NSObject<MKAnnotation>
    
    @property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
    @property (nonatomic,copy,readonly) NSString *title;
    @property (nonatomic,copy,readonly) NSString *subtitle;
    
    @property (nonatomic,strong) UIImage *image;
    
    //view 左侧的图片
    @property (nonatomic,strong) UIImage *showCoverImage;
    @property (nonatomic,copy) NSString *detail;
    //view 右侧的图片
    @property (nonatomic,strong) UIImage *detailImage;
    
    - (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate withTitle:(NSString *)title withSubTitle:(NSString *)subTitle;
    
    @end
    
    @class ArtistAnnotation;
    @interface ArtistAnnotationView : MKAnnotationView
    
    - (instancetype)initWithArtistAnnotation:(ArtistAnnotation *)artistAnnotation reuseIdentifier:(NSString *)reuseIdentifier;
    
    - (void)setArtistAnnotation:(ArtistAnnotation *)artistAnnotation;
    
    @end
    

    其他代理方法

    -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {
        NSLog(@"选中:%@", view.annotation.title);
    }
    
    -(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
    {
        NSLog(@"取消选中:%@", view.annotation.title);
    }
    
    -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState
    {
        NSLog(@"%zd:%zd", oldState, newState);
    }
    

    路线绘制

    绘制两个点之间的路线,涉及的相关类

    MKMapItem //起点、终点
    MKDirections 
    MKDirectionsRequest
    MKPolyline
    
    //线路的绘制
    - (void)lineDrawing {
        MKPlacemark *fromPlacemark = [[MKPlacemark alloc] initWithCoordinate:_fromCoordinate addressDictionary:nil];
        MKPlacemark *toPlacemark = [[MKPlacemark alloc] initWithCoordinate:_toCoordinate addressDictionary:nil];
        MKMapItem *fromItem = [[MKMapItem alloc] initWithPlacemark:fromPlacemark];
        MKMapItem *toItem = [[MKMapItem alloc] initWithPlacemark:toPlacemark];
        MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
        request.source = fromItem;
        request.destination = toItem;
        request.requestsAlternateRoutes = YES;
        //交通方式
        request.transportType = MKDirectionsTransportTypeWalking;
        MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
        [directions calculateDirectionsWithCompletionHandler:
         ^(MKDirectionsResponse *response, NSError *error) {
             if (error) {
                 NSLog(@"error:%@", error);
             }
             else {
                 MKRoute *route = response.routes[0];
                 [_mapView addOverlay:route.polyline];
             }
         }];
        
    }
    
    //线路的绘制
    - (MKOverlayRenderer *)mapView:(MKMapView *)mapView
                rendererForOverlay:(id<MKOverlay>)overlay
    {
        MKPolylineRenderer *renderer;
        renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        renderer.lineWidth = 5.0;
        renderer.strokeColor = [UIColor purpleColor];
        
        return renderer;
    }
    

    demo演示

    demo演示

    参考资料

    让MKMapView变得丰富多彩
    ios开发之Mapkit
    iOS开发系列--地图与定位
    【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等
    Maps for Developers
    斯坦福公开课mapkit
    利用MapKit实现路线查询功能

    相关文章

      网友评论

          本文标题:MapKit

          本文链接:https://www.haomeiwen.com/subject/loousttx.html