美文网首页
iOS 开发 高德地图-----初见

iOS 开发 高德地图-----初见

作者: 未完成1307 | 来源:发表于2018-01-15 17:16 被阅读240次
    c08b3cd7cc3000756029d5229ebdec2b.jpg

    小菜鸟的内心独白: 关于地图类的SDK 起初是比较排排斥的 虽然很多人都说“你看文档嘛 文档上面什么都有”。。。。。。。。。。。。但我内内心还是拒绝的 PS: 不知道是不是路痴的原因。。。。。。。。。

    这里记录一下 常见的方法 以便自己下次要用的时候 ,又很惶恐

    1 、获取key之类的,高德的文档写的很详细, 也就不多加累赘了。
    2、 配置权限 Privacy - Location Always Usage Description 始终允许访问位置信息
    // Privacy - Location Usage Description 永不允许访问位置信息
    // Privacy - Location When In Use Usage Description 使用应用期间允许访问位置信息

    3、配置高德 Key 添加开启 HTTPS 功能
    [[AMapServices sharedServices] setEnableHTTPS:YES];

    4、 [AMapServices sharedServices].apiKey = @"你的key";
    5、以下是常用方法

    • 关于蓝点
        self.mapView.showsUserLocation = YES;
        self.mapView.userTrackingMode = MAUserTrackingModeFollow;
        self.mapView.userLocation.title = @"您的位置在这里";
        //显示室内地图
         self.mapView.showsIndoorMap = YES;
        //地图类型
        self.mapView.mapType = MAMapTypeStandard;
        ///地图类型
        typedef NS_ENUM(NSInteger, MAMapType)
        {
            MAMapTypeStandard = 0,  ///< 普通地图
            MAMapTypeSatellite,     ///< 卫星地图
            MAMapTypeStandardNight, ///< 夜间视图
            MAMapTypeNavi,          ///< 导航视图
            MAMapTypeBus            ///< 公交视图
        };
        
        ///是否开启自定义样式, 默认NO. since 5.0.0
        self.mapView.customMapStyleEnabled = NO;
        
        //自定义蓝点
        MAUserLocationRepresentation *represent = [[MAUserLocationRepresentation alloc] init];
        ///精度圈是否显示,默认YES
        represent.showsAccuracyRing = YES;
        ///是否显示方向指示(MAUserTrackingModeFollowWithHeading模式开启)。默认为YES
        represent.showsHeadingIndicator = YES;
        ///精度圈 填充颜色, 默认 kAccuracyCircleDefaultColor
        represent.fillColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:.3];
        ///精度圈 边线颜色, 默认 kAccuracyCircleDefaultColor
        represent.strokeColor = [UIColor redColor];
        
        represent.lineWidth = 22.f;
        ///定位图标, 与蓝色原点互斥
        represent.image = [UIImage imageNamed:@"1.jpg"];
            ///内部蓝色圆点是否使用律动效果, 默认YES
        represent.enablePulseAnnimation = YES;
        /**
         * @brief 设定UserLocationView样式。如果用户自定义了userlocation的annotationView,或者该annotationView还未添加到地图上,此方法将不起作用
         * @param representation 样式信息对象
         */
        [self.mapView updateUserLocationRepresentation:represent];
    
    • 关于交互
          //跳转logo位置 地图Logo不能移除
        _mapView.logoCenter = CGPointMake(CGRectGetWidth(self.view.bounds)-55, 450);
        //指南针
       
        _mapView.showsCompass= YES; // 设置成NO表示关闭指南针;YES表示显示指南针
        _mapView.compassOrigin= CGPointMake(_mapView.compassOrigin.x, 22);
        
        //比例尺
        _mapView.showsScale= YES;  //设置成NO表示不显示比例尺;YES表示显示比例尺
        _mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x, 22);  //设置比例尺位置
        
        
        //缩放
        [self.mapView setZoomLevel:2 animated:YES];
        
        //使用手势不能改变camera的角度,但通过接口还是可以改变的 (倾斜)
        [_mapView setCameraDegree:30.f animated:YES duration:0.5];
        self.mapView.rotateCameraEnabled = NO;
        
        
        ///是否支持平移, 默认YES
        self.mapView.scrollEnabled = YES;
        ///是否支持缩放
        self.mapView.zoomEnabled = YES;
        //旋转
        self.mapView.rotateEnabled = YES;
        
           //指定中心点
        MAMapStatus * status = [self.mapView getMapStatus];
        //screenAnchor 视图锚点  当前地图的视图中心
        status.screenAnchor = CGPointMake(0.5, 0.5);
        ///设置地图旋转角度(逆时针为正向), 单位度, [0,360)
        status.rotationDegree = 135.f;
        ///设置地图相机角度(范围为[0.f, 45.f])
        status.cameraDegree = 60.f;
        [self.mapView setMapStatus:status animated:YES];
    
    • 截图
       CGRect inRect = [self.view convertRect:CGPathGetPathBoundingBox(self.shapeLayer.path)
                                       toView:self.mapView];
       [self.mapView takeSnapshotInRect:inRect withCompletionBlock:^(UIImage *resultImage, NSInteger state) {
          // state表示地图此时是否完整,0-不完整,1-完整
       }];
    //此外官方demo中的方法
    - (void)panGesture:(UIPanGestureRecognizer *)panGesture
       {
           static CGPoint startPoint;
           //识别器已经接收识别为此手势(状态)的触摸(Began)
           if (panGesture.state == UIGestureRecognizerStateBegan)
           {
               self.shapeLayer.path = NULL;
               
               startPoint = [panGesture locationInView:self.view];
           }
           //识别器已经接收到触摸,并且识别为手势改变(Changed)
           else if (panGesture.state == UIGestureRecognizerStateChanged)
           {
               CGPoint currentPoint = [panGesture locationInView:self.view];
               CGPathRef path = CGPathCreateWithRect(CGRectMake(startPoint.x, startPoint.y, currentPoint.x - startPoint.x, currentPoint.y - startPoint.y), NULL);
               self.shapeLayer.path = path;
               CGPathRelease(path);
           }
       }
    
    • POI
     // 是否支持单击地图获取POI信息(默认为YES)
       self.mapView.touchPOIEnabled = YES;
       
       MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
       
       annotation.coordinate = touchPoi.coordinate;
       annotation.title      = touchPoi.name;
    
       //是否固定在屏幕一点,
       annotation.lockedToScreen = YES;
       ///固定屏幕点的坐标
       annotation.lockedScreenPoint = CGPointMake(100, 100);
       
       annotation.subtitle = @"副标题";
       /* Remove prior annotation. */
       //移除原来 的 (不移除话 就可以插入多个大头针)
       [self.mapView removeAnnotation:self.poiAnnotation];
       //
       [self.mapView addAnnotation:annotation];
       //选中对应的那个
       [self.mapView selectAnnotation:annotation animated:YES];
       //关于MAPinAnnotationView
        static NSString *touchPoiReuseIndetifier = @"touchPoiReuseIndetifier";
       MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:touchPoiReuseIndetifier];
       annotationView.backgroundColor = [UIColor orangeColor];
       if (annotationView == nil)
       {
           annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation
                                                            reuseIdentifier:touchPoiReuseIndetifier];
       }
       ///是否允许弹出callout
       annotationView.canShowCallout = YES;
       ///添加到地图时是否使用下落动画效果
       annotationView.animatesDrop   = YES;
       annotationView.draggable      = YES;
       //枚举
       annotationView.pinColor = MAPinAnnotationColorPurple;
       annotationView.image = [UIImage imageNamed:@"restaurant"];
       
       //可自定义view  详见官方demo
    
    • 常用方法
    
    //蓝点箭头旋转
    - (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
    {
        if (!updatingLocation && self.userLocationAnnotationView != nil)
        {
            [UIView animateWithDuration:0.1 animations:^{
                ///rotationDegree 设置地图旋转角度(逆时针为正向)
                double degree = userLocation.heading.trueHeading - self.mapView.rotationDegree;
                self.userLocationAnnotationView.transform = CGAffineTransformMakeRotation(degree * M_PI / 180.f );
                
            }];
        }
    }
    /**
     * @brief 根据经纬度坐标数据生成多段线
     * @param coords 经纬度坐标数据,coords对应的内存会拷贝,调用者负责该内存的释放
     * @param count  经纬度坐标个数
     * @return 生成的多段线
     */
    + (instancetype)polylineWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count;
    
    /**
     @brief 添加移动动画, 第一个添加的动画以当前coordinate为起始点,沿传入的coordinates点移动,否则以上一个动画终点为起始点. since 4.5.0
     @param coordinates c数组,由调用者负责coordinates指向内存的管理
     @param count coordinates数组大小
     @param duration 动画时长,0或<0为无动画
     @param name 名字,如不指定可传nil
     @param completeCallback 动画完成回调,isFinished: 动画是否执行完成
     */
    - (MAAnnotationMoveAnimation *)addMoveAnimationWithKeyCoordinates:(CLLocationCoordinate2D *)coordinates
                                                                count:(NSUInteger)count
                                                         withDuration:(CGFloat)duration
                                                             withName:(NSString *)name
                                                     completeCallback:(void(^)(BOOL isFinished))completeCallback;
    
    
    /**
     * @brief 分段绘制,根据经纬度坐标数据生成多段线
     *
     * 分段纹理绘制:其对应的MAMultiTexturePolylineRenderer必须使用 - (BOOL)loadStrokeTextureImages:(NSArray *)textureImages; 加载纹理图片, 否则使用默认的灰色纹理绘制
     *
     * 分段颜色绘制:其对应的MAMultiColoredPolylineRenderer必须设置strokeColors属性
     *
     * @param coords           指定的经纬度坐标点数组,注意:如果有连续重复点,需要去重处理,只保留一个,否则会导致绘制有问题。
     * @param count            坐标点的个数
     * @param drawStyleIndexes 纹理索引数组(颜色索引数组), 成员为NSNumber, 且为非负数。例子:[1,3,6] 表示 0-1使用第一种颜色\纹理,1-3使用第二种,3-6使用第三种,6-最后使用第四种
     * @return 生成的折线对象
     */
    + (instancetype)polylineWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count drawStyleIndexes:(NSArray*) drawStyleIndexes;
    /**
         * @brief 根据经纬度生成大地曲线
         * @param coords 经纬度
         * @param count  点的个数
         * @return 生成的大地曲线
         */
        self.geodesicPolyline = [MAGeodesicPolyline polylineWithCoordinates:geodesicCoords
                                                                      count:(sizeof(geodesicCoords)/sizeof(CLLocationCoordinate2D))];
        
    /**
     * @brief 长按地图,返回经纬度
     * @param mapView 地图View
     * @param coordinate 经纬度
     */
    - (void)mapView:(MAMapView *)mapView didLongPressedAtCoordinate:(CLLocationCoordinate2D)coordinate;
    
    
    • 常见类
    1. overlay 轨迹回放  MATraceReplayOverlayRenderer ///轨迹回放overlay渲染器
    
    2.MACircleRenderer 类是MACircle的显示圆Renderer
    
    3.MAPolyline 此类用于定义一个由多个点相连的多段线,点与点之间尾部相连但第一点与最后一个点不相连, 通常MAPolyline是MAPolylineView的model  
    MAPolyline * polyline = [MAPolyline polylineWithPoints:points count:5];
    //    [self.overlays addObject:polyline];
    //        [self.mapView addOverlays:self.overlays];
    
    4. MAPolylineRenderer  此类用于绘制MAPolyline
    
    5.MAOverlayPathRenderer  该类设置overlay绘制的属性,可以使用该类的子类MACircleRenderer, MAPolylineRenderer, MAPolygonRenderer或者继承该类
    
    6.MACircle 该类用于定义一个圆 
       MACircle *circle1 = [MACircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(39.996441, 116.411146) radius:15000];
    MACircleRenderer 该类是MACircle的显示圆Renderer
    
    7. MAPolygon 此类用于定义一个由多个点组成的闭合多边形
    //      MAPolygon *polygon = [MAPolygon polygonWithCoordinates:coordinates count:4];
       MAPolygonRenderer 此类用于绘制MAPolygon
    
    8.  MAMultiPolyline//此类用于定义一个由多个点相连的多段线,绘制时支持分段采用不同颜色(纹理)绘制,点与点之间尾部相连但第一点与最后一个点不相连, 通常MAMultiPolyline是MAMultiColoredPolylineRenderer(分段颜色绘制)或MAMultiTexturePolylineRenderer(分段纹理绘制)的model
     MAMultiPolyline *coloredPolyline = [MAMultiPolyline polylineWithCoordinates:coloredPolylineCoords count:5 drawStyleIndexes:@[@1, @3]];
         ///此类用于绘制MAMultiPolyline对应的多段线,支持分段颜色绘制
        MAMultiColoredPolylineRenderer * polylineRenderer;  
        ///颜色是否渐变
        polylineRenderer.gradient = YES;
         ///此类用于绘制MAMultiPolyline对应的多段线,支持分段纹理绘制
        MAMultiTexturePolylineRenderer * polylineRenderer ;
        UIImage * bad = [UIImage imageNamed:@"custtexture_bad"];
        ///分段纹理图片数组
        polylineRenderer.strokeTextureImages = @[bad];
    

    其实真的就是那句话:看文档 看文档 (对自己说)。。。。。。

    相关文章

      网友评论

          本文标题:iOS 开发 高德地图-----初见

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