iOS 开发iOSiOS开发收集

iOS笔记-地图的基本使用

2016-03-07  本文已影响2557人  Developer_Yancy

地图的基本使用

-(CLLocationManager *)locationM
{
if (!_locationM) {
_locationM = [[CLLocationManager alloc] init];
if ([_locationM respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[_locationM requestAlwaysAuthorization];
}
}
return _locationM;
}
```

- 2.设置地图的用户追踪模式
```objc
    /**
                    MKUserTrackingModeNone = 0, // 不跟随
                    MKUserTrackingModeFollow, // 跟随用户位置
                    MKUserTrackingModeFollowWithHeading, // 跟随用户位置,并跟随用户方向
                 */
                [self locationM];
                self.customMapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;

```

地图中级使用

-(void)mapView:(MKMapView )mapView didUpdateUserLocation:(MKUserLocation )userLocation
{
/

MKUserLocation : 被称作“大头针模型”,其实喊什么都行,本质就是一个数据模型,只不过此模型遵循了大头针要遵循的协议(MKAnnotation)
location: 用户当前所在位置信息(CLLocation对象)
title: 大头针标注要显示的标题(NSString对象)
subtitle: 大头针标注要显示的子标题(NSString对象)
/
// 根据用户当前位置的经纬度,设置地图显示中心
/
*
存在弊端:地图显示比例过大,无法调整
解决方案:直接使用对应的调整地图“显示区域”的API
/
// [mapView setCenterCoordinate:userLocation.coordinate animated:YES];
/
*
MKCoordinateSpan 跨度解释:
latitudeDelta:纬度跨度,因为南北纬各90度,所以此值的范围是(0---180);此值表示,整个地图视图宽度,显示多大跨度
longitudeDelta:经度跨度,因为东西经各180度,所以此值范围是(0---360):此值表示,整个地图视图高度,显示多大跨度
注意:地图视图显示,不会更改地图的比例,会以地图视图高度或宽度较小的那个为基准,按比例调整
*/
// MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
// MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.coordinate, span);
// [mapView setRegion:region animated:YES];
}

    // 当地图区域(跨度)改变时调用
    -(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
    {
        NSLog(@"%f---%f", mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
    }

```

大头针基本使用

自定义大头针

大头针图标,大头针标注,左侧视图,右侧视图,详情视图,等;

    ```
- 选中和取消选中大头针时的代理方法
    ```objc
    // 点击标注
    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
        NSLog(@"点击标注");
    }
    // 选中大头针
    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {
        NSLog(@"选中大头针");
    }
    // 取消选中大头针
    -(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
    {
        NSLog(@"取消选中大头针");
    }
    ```

利用系统App导航

// 根据两个地标对象进行调用系统导航
- (void)beginNavWithBeginPlacemark:(CLPlacemark *)beginPlacemark andEndPlacemark:(CLPlacemark *)endPlacemark
{

    // 根据 CLPlacemark 地标对象创建 MKPlacemark 地标对象
    MKPlacemark *itemP1 = [[MKPlacemark alloc] initWithPlacemark:beginPlacemark];
    MKMapItem *item1 = [[MKMapItem alloc] initWithPlacemark:itemP1];


    MKPlacemark *itemP2 = [[MKPlacemark alloc] initWithPlacemark:endPlacemark];
    MKMapItem *item2 = [[MKMapItem alloc] initWithPlacemark:itemP2];

    NSDictionary *launchDic = @{
                                // 设置导航模式参数
                                MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                                // 设置地图类型
                                MKLaunchOptionsMapTypeKey : @(MKMapTypeHybridFlyover),
                                // 设置是否显示交通
                                MKLaunchOptionsShowsTrafficKey : @(YES),

                                };
    // 根据 MKMapItem 数组 和 启动参数字典 来调用系统地图进行导航
    [MKMapItem openMapsWithItems:@[item1, item2] launchOptions:launchDic];

}

数字版街景地图

    /**
        补充1:类似于地图街景,增强用户体验
     */
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(23.132931, 113.375924);
    MKMapCamera *camera = [MKMapCamera cameraLookingAtCenterCoordinate:center fromEyeCoordinate:CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001) eyeAltitude:1];
    self.mapView.camera = camera;

地图快照截图

    /**
        补充2:地图截图
     */
    // 截图附加选项
    MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
    // 设置截图区域(在地图上的区域,作用在地图)
    options.region = self.mapView.region;
//    options.mapRect = self.mapView.visibleMapRect;

    // 设置截图后的图片大小(作用在输出图像)
    options.size = self.mapView.frame.size;
    // 设置截图后的图片比例(默认是屏幕比例, 作用在输出图像)
    options.scale = [[UIScreen mainScreen] scale];

    MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
    [snapshotter startWithCompletionHandler:^(MKMapSnapshot * _Nullable snapshot, NSError * _Nullable error) {
        if (error) {
            NSLog(@"截图错误:%@",error.localizedDescription);
        }else
        {
            // 设置屏幕上图片显示
            self.snapshootImageView.image = snapshot.image;
            // 将图片保存到指定路径(此处是桌面路径,需要根据个人电脑不同进行修改)
            NSData *data = UIImagePNGRepresentation(snapshot.image);
            [data writeToFile:@"/Users/chenyanxiang/Desktop/snap.png" atomically:YES];
        }
    }];

获取导航路线信息

// 根据两个地标,向苹果服务器请求对应的行走路线信息
- (void)directionsWithBeginPlackmark:(CLPlacemark *)beginP andEndPlacemark:(CLPlacemark *)endP
{

    // 创建请求
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

    // 设置开始地标
    MKPlacemark *beginMP = [[MKPlacemark alloc] initWithPlacemark:beginP];
    request.source = [[MKMapItem alloc] initWithPlacemark:beginMP];

    // 设置结束地标
    MKPlacemark *endMP = [[MKPlacemark alloc] initWithPlacemark:endP];
    request.destination = [[MKMapItem alloc] initWithPlacemark:endMP];


    // 根据请求,获取实际路线信息
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {

        /**
         MKDirectionsResponse对象解析
            source :开始位置
            destination :结束位置
            routes : 路线信息 (MKRoute对象)

         MKRoute对象解析
            name : 路的名称
            advisoryNotices : 注意警告信息
            distance : 路线长度(实际物理距离,单位是m)
            polyline : 路线对应的在地图上的几何线路(由很多点组成,可绘制在地图上)
            steps : 多个行走步骤组成的数组(例如“前方路口左转”,“保持直行”等等, MKRouteStep 对象)

        MKRouteStep对象解析
            instructions : 步骤说明(例如“前方路口左转”,“保持直行”等等)
            transportType : 通过方式(驾车,步行等)
            polyline : 路线对应的在地图上的几何线路(由很多点组成,可绘制在地图上)

        注意:
            MKRoute是一整条长路;MKRouteStep是这条长路中的每一截;

         */
        [response.routes enumerateObjectsUsingBlock:^(MKRoute * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"%@--", obj.name);
            [obj.steps enumerateObjectsUsingBlock:^(MKRouteStep * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSLog(@"%@", obj.instructions);
            }];
        }];

    }];

}

绘制导航路线

上一篇 下一篇

猜你喜欢

热点阅读