美文网首页
iOS 百度地图 功能整理

iOS 百度地图 功能整理

作者: 何以_aaa | 来源:发表于2017-10-12 16:20 被阅读158次

    1 地图属性

    1. 地图属性
      mapType :可选卫星,标准矢量图,空白
    2. 缩放
      zoomEnabled 是否可缩放
      zoomLevel 比例尺级别 :手机上可使用的级别为3-21级
    3. 是否可移动
      scrollEnabled
    4. 旋转角度
      rotation:-180~180
    5. 俯视角度
      overlooking:-45~0
    6. 百度图标位置
      logoPosition
    7. 是否展示路况图层
      [mapView setTrafficEnabled:YES];
    8. 是否展示3D楼宇
      [mapView setBuildingsEnabled:YES];
    9. 是否展示热力图
      [mapView setBaiduHeatMapEnabled:YES];
    10. 是否展示底图标注
      [mapView setShowMapPoi:YES];
    11. 比例尺
      showMapScaleBar 是否展示比例尺
      mapScaleBarPosition 比例尺的位置
      mapScaleBarSize 比例尺的宽高
    12. 是否支持所有手势
      gesturesEnabled
    13. 双击手势放大地图时, 地图中心点移动至点击处
      ChangeCenterWithDoubleTouchPointEnabled
    14. 指南针位置
      [mapView setCompassPosition:point];
    15. 定位模式
      userTrackingMode :
      普通模式(小蓝点)BMKUserTrackingModeNone
      跟随模式(蓝箭头) BMKUserTrackingModeFollow
      罗盘模式 BMKUserTrackingModeFollowWithHeading

    2 自定义地图样式

    控制器初始化时,设置地图样式,之后可随时控制打开/关闭个性化

    + (void)initialize {
        //设置自定义地图样式,会影响所有地图实例
        //注:必须在BMKMapView对象初始化之前调用
        NSString* path = [[NSBundle mainBundle] pathForResource:@"custom_config_清新蓝" ofType:@""];
        [BMKMapView customMapStyle:path];
    }
    
    //打开/关闭个性化地图
        [BMKMapView enableCustomMapStyle:YES];
    

    custom_config_清新蓝 样式文件:

    [
      {
        "featureType": "land",
        "elementType": "geometry",
        "stylers": {
          "color": "#e7f7fc",
          "visibility": "on"
        }
      },
      {
        "featureType": "water",
        "elementType": "all",
        "stylers": {
          "color": "#96b5d6",
          "visibility": "on"
        }
      },
      {
        "featureType": "green",
        "elementType": "all",
        "stylers": {
          "color": "#b0d3dd",
          "visibility": "on"
        }
      },
      {
        "featureType": "highway",
        "elementType": "geometry.fill",
        "stylers": {
          "color": "#a6cfcf"
        }
      },
      {
        "featureType": "highway",
        "elementType": "geometry.stroke",
        "stylers": {
          "color": "#7dabb3"
        }
      },
      {
        "featureType": "arterial",
        "elementType": "geometry.fill",
        "stylers": {
          "color": "#A6EBFF"
        }
      },
      {
        "featureType": "arterial",
        "elementType": "geometry.stroke",
        "stylers": {
          "color": "#9ECFFF"
        }
      },
        {
          "featureType": "local",
          "elementType": "geometry.fill",
          "stylers": {
            "color": "#e7f7fc"
          }
        },
        {
          "featureType": "local",
          "elementType": "geometry.stroke",
          "stylers": {
            "color": "#50da53"
          }
        },
      {
        "featureType": "poilabel",
        "elementType": "labels.text.fill",
        "stylers": {
          "color": "#374a46",
          "visibility": "on"
        }
      },
      {
        "featureType": "poilabel",
        "elementType": "labels.text.stroke",
        "stylers": {
          "color": "#e9eeed",
          "visibility": "on"
        }
      }
    ]
    

    3 手势

    3.1 系统手势

    地图手势通过 mapView 的代理 BMKMapViewDelegate 的代理方法回调

    • 点中底图标注后会回调此接口
    - (void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi*)mapPoi
    {
        NSLog(@"onClickedMapPoi-%@",mapPoi.text);
        NSString* showmeg = [NSString stringWithFormat:@"您点击了底图标注:%@,\r\n当前经度:%f,当前纬度:%f,\r\nZoomLevel=%d;RotateAngle=%d;OverlookAngle=%d", mapPoi.text,mapPoi.pt.longitude,mapPoi.pt.latitude, (int)_mapView.zoomLevel,_mapView.rotation,_mapView.overlooking];
        _showMsgLabel.text = showmeg;
    }
    
    • 点中底图空白处会回调此接口
    - (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate
    {
        NSLog(@"onClickedMapBlank-latitude==%f,longitude==%f",coordinate.latitude,coordinate.longitude);
        NSString* showmeg = [NSString stringWithFormat:@"您点击了地图空白处(blank click).\r\n当前经度:%f,当前纬度:%f,\r\nZoomLevel=%d;RotateAngle=%d;OverlookAngle=%d", coordinate.longitude,coordinate.latitude,
                             (int)_mapView.zoomLevel,_mapView.rotation,_mapView.overlooking];
        _showMsgLabel.text = showmeg;
    }
    
    • 双击地图时会回调此接口
    - (void)mapview:(BMKMapView *)mapView onDoubleClick:(CLLocationCoordinate2D)coordinate
    {
        NSLog(@"onDoubleClick-latitude==%f,longitude==%f",coordinate.latitude,coordinate.longitude);
        NSString* showmeg = [NSString stringWithFormat:@"您双击了地图(double click).\r\n当前经度:%f,当前纬度:%f,\r\nZoomLevel=%d;RotateAngle=%d;OverlookAngle=%d", coordinate.longitude,coordinate.latitude,
                             (int)_mapView.zoomLevel,_mapView.rotation,_mapView.overlooking];
        _showMsgLabel.text = showmeg;
    }
    
    • 长按地图时会回调此接口
    - (void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate
    {
        NSLog(@"onLongClick-latitude==%f,longitude==%f",coordinate.latitude,coordinate.longitude);
        NSString* showmeg = [NSString stringWithFormat:@"您长按了地图(long pressed).\r\n当前经度:%f,当前纬度:%f,\r\nZoomLevel=%d;RotateAngle=%d;OverlookAngle=%d", coordinate.longitude,coordinate.latitude,
                             (int)_mapView.zoomLevel,_mapView.rotation,_mapView.overlooking];
        _showMsgLabel.text = showmeg;
    
    }
    
    • 地图展示区域发生变化
    - (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
    {
        NSString* showmeg = [NSString stringWithFormat:@"地图区域发生了变化(x=%d,y=%d,\r\nwidth=%d,height=%d).\r\nZoomLevel=%d;RotateAngle=%d;OverlookAngle=%d",(int)_mapView.visibleMapRect.origin.x,(int)_mapView.visibleMapRect.origin.y,(int)_mapView.visibleMapRect.size.width,(int)_mapView.visibleMapRect.size.height,(int)_mapView.zoomLevel,_mapView.rotation,_mapView.overlooking];
        
        _showMsgLabel.text = showmeg;
    }
    

    3.2 自定义手势

    - (void)addCustomGestures {
        /*
         *注意:
         *添加自定义手势时,必须设置UIGestureRecognizer的属性cancelsTouchesInView 和 delaysTouchesEnded 为NO,
         *否则影响地图内部的手势处理
         */
        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
        doubleTap.delegate = self;
        doubleTap.numberOfTapsRequired = 2;
        doubleTap.cancelsTouchesInView = NO;
        doubleTap.delaysTouchesEnded = NO;
        
        [self.view addGestureRecognizer:doubleTap];
        
        /*
         *注意:
         *添加自定义手势时,必须设置UIGestureRecognizer的属性cancelsTouchesInView 和 delaysTouchesEnded 为NO,
         *否则影响地图内部的手势处理
         */
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        singleTap.delegate = self;
        singleTap.cancelsTouchesInView = NO;
        singleTap.delaysTouchesEnded = NO;
        [singleTap requireGestureRecognizerToFail:doubleTap];
        [self.view addGestureRecognizer:singleTap];
    }
    
    - (void)handleSingleTap:(UITapGestureRecognizer *)theSingleTap {
        /*
         *do something
         */
        NSLog(@"my handleSingleTap");
    }
    
    - (void)handleDoubleTap:(UITapGestureRecognizer *)theDoubleTap {
        /*
         *do something
         */
        NSLog(@"my handleDoubleTap");
    }
    

    4 地图截图并保存

    //截图
    -(void)snapshot
        _imgView.image = [_mapView takeSnapshot];
        [self saveImageToPhotos:_imgView.image];
    }
    
    - (void)saveImageToPhotos:(UIImage*)savedImage
    {
        UIImageWriteToSavedPhotosAlbum(savedImage,self,@selector(image:didFinishSavingWithError:contextInfo:),NULL);
    }
    
    // 指定回调方法
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
        NSString *msg = nil ;
        if(error != nil && image != nil) {
            msg = @"保存图片失败" ;
        } else {
            msg = @"保存图片成功" ;
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
                                                        message:msg
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    }
    

    相关文章

      网友评论

          本文标题:iOS 百度地图 功能整理

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