高德地图

作者: 陪你看日出去 | 来源:发表于2016-07-13 18:41 被阅读1560次

    #pragma mark - 创建地图

    -(void)createMapView
    {
        _mapView = [[MAMapView alloc]initWithFrame:self.view.frame];
        [self.view addSubview:_mapView];
        //设置代理
        _mapView.delegate = self;
        /*高德地图涉及到的一些属性*/
        //1.设置是否显示底层的建筑名称标注
        _mapView.showsLabels = YES;
        //2.设置地图的类型,分为卫星地图,标准地图和夜景地图
        _mapView.mapType = MAMapTypeStandard;
        //3.设置是否显示交通状况
        _mapView.showTraffic = YES;
        //4.设置显示本人的位置,结合定位功能使用
        _mapView.showsUserLocation = YES;
        //5.设置地图logo,默认字样是“高德地图”,用logoCenter来设置logo的位置
        _mapView.logoCenter = CGPointMake(self.view.frame.size.width - 100, self.view.frame.size.height - 50);
        //6.设置指南针compass,默认是开启状态,大小是定值,显示在地图的右上角
        _mapView.showsCompass = YES;
        _mapView.compassOrigin = self.view.center;
       //7.设置比例尺scale,默认显示在地图的左上角
       _mapView.showsScale = YES;
       _mapView.scaleOrigin = CGPointMake(100, 100);
       //8.设置地图手势
       _mapView.zoomEnabled = YES;//开启地图手势
       //[_mapView setZoomLevel:15 animated:YES];
       //9.设置是否开启滑动手势
       _mapView.scrollEnabled = YES;
       //10.设置是否开启旋转手势
       _mapView.rotateEnabled = YES;
       //11.设置是否开启倾斜手势
       _mapView.rotateCameraEnabled = YES;
    }
    

    #pragma mark - 在地图上添加大头针标注

    -(void)addAnnotation
    {
        MAPointAnnotation * pointAnnotation = [[MAPointAnnotation alloc]init];
        //设置大头针需要标记的位置
        pointAnnotation.coordinate = CLLocationCoordinate2DMake(39.989631, 116.481018);
        //设置标题
        pointAnnotation.title = @"方恒国际";
        //设置副标题
        pointAnnotation.subtitle = @"这是我家";
        [_mapView addAnnotation:pointAnnotation];
    }
    

    #pragma mark - 设置大头针的代理方法

    -(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
    {
       /*
        //1.用系统的大头针样式
        //大头针也涉及到复用,它的复用跟UITableViewCell的复用机制一样
        //面向对象语言的特性:封装,继承,多态(父类的指针可以指向子类的对象)
        MAPinAnnotationView * pinAnotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"mapView"];
        //判断复用池中是否有可用的对象,如果没有则创建
        if (!pinAnotationView) {
            pinAnotationView = [[MAPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"mapView"];
        }
        //设置气泡可以显示
        pinAnotationView.canShowCallout = YES;
        //设置大头针降落的动画
        pinAnotationView.animatesDrop = YES;
        //设置大头针的颜色
        pinAnotationView.pinColor = MAPinAnnotationColorPurple;
        //设置大头针是否可以拖拽
        pinAnotationView.draggable = YES;
        return pinAnotationView;
     */
    
    
         //2.使用自定义的大头针样式
         MAAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"mapView"];
         if (!annotationView) {
            annotationView = [[MAAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"mapView"];
         }
    
        //设置大头针的样式为一张图片
        annotationView.image = [UIImage imageNamed:@"pig1"];
        //让气泡显示
        annotationView.canShowCallout = YES;
        //设置是否拖拽
        annotationView.draggable = YES;
        return annotationView;
    }
    

    #pragma mark- 画折线 ,一般用于路径规划

    -(void)drawLine
    {
        //构造折线的数据点
        CLLocationCoordinate2D commonPolylineCoords[4];
        commonPolylineCoords[0].latitude = 39.832136;
        commonPolylineCoords[0].longitude = 116.34095;
        commonPolylineCoords[1].latitude = 39.832136;
        commonPolylineCoords[1].longitude = 116.42095;
        commonPolylineCoords[2].latitude = 39.902136;
        commonPolylineCoords[2].longitude = 116.42095;
    
        commonPolylineCoords[3].latitude = 39.902136;
        commonPolylineCoords[3].longitude = 116.44095;
    
        //构造折线对象
        //参数一:多个点组成的类似数组的经纬度数据
        //参数二:构成折线总共涉及到几个点
        MAPolyline * polyLine = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:4];
        //将覆盖图层添加到地图上
        [_mapView addOverlay:polyLine];
    }
    

    #pragma mark - 画多边形,一般用于精确的搜索

    -(void)drawGon
    {
        //构造多边形数据
        CLLocationCoordinate2D coordinates[4];
        coordinates[0].latitude = 39.810892;
        coordinates[0].longitude = 116.233413;
    
        coordinates[1].latitude = 39.816600;
        coordinates[1].longitude = 116.331842;
    
        coordinates[2].latitude = 39.762187;
        coordinates[2].longitude = 116.357932;
    
        coordinates[3].latitude = 39.733653;
        coordinates[3].longitude = 116.278255;
    
        MAPolygon * polyGon = [MAPolygon polygonWithCoordinates:coordinates count:4];
       [_mapView addOverlay:polyGon];
    }
    

    #pragma mark - 画圆,一般用于模糊搜索

    -(void)drawCircle
    {
        //需要原点和半径
        MACircle * circle = [MACircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(39.952136, 116.50095) radius:5000];
        [_mapView addOverlay:circle];
    }
    

    #pragma mark - 实现覆盖图层样式的代理方法

    -(MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay
    {
         //旧版的返回值是MAOverLayView,新版的是MAOverlayRenderer
         //画折线
         if ([overlay isKindOfClass:[MAPolyline class]]) {
               MAPolylineRenderer * polyLine = [[MAPolylineRenderer alloc]initWithPolyline:overlay];
               //设置属性
               //设置线宽
               polyLine.lineWidth = 5;
               //设置填充颜色
               polyLine.fillColor = [UIColor redColor];
               //设置笔触颜色
               polyLine.strokeColor = [UIColor greenColor];
               //设置断点类型
               polyLine.lineCapType = kMALineCapRound;
               return polyLine;
          } else if([overlay isKindOfClass:[MAPolygon class]])
          {
              MAPolygonRenderer * polyGon = [[MAPolygonRenderer alloc]initWithPolygon:overlay];
              polyGon.fillColor = [UIColor redColor];
              polyGon.strokeColor = [UIColor yellowColor];
              //设置显示未虚线
              polyGon.lineDash = YES;
              polyGon.lineWidth = 8;
              return polyGon;
          } else if([overlay isKindOfClass:[MACircle class   ]])
          {
              MACircleRenderer * circle = [[MACircleRenderer alloc]initWithCircle:overlay];
              circle.fillColor = [UIColor blueColor];
              circle.strokeColor = [UIColor yellowColor];
              circle.lineWidth = 5;
              circle.lineDash = YES;
              return circle;
          }
          return nil;
    }
    

    定位

    - (void)viewDidLoad {
         [super viewDidLoad];
         //配置用户key
         [AMapLocationServices sharedServices].apiKey = @"e51bdf2a028ff1a08da5f20f114ff3c0";
         //初始化定位
         _manager = [[AMapLocationManager alloc]init];
         //设置代理
         _manager.delegate = self;
         //开启定位
        [_manager startUpdatingLocation];
    }
    //定位的目的是为了拿到经纬度值,iOS8之后更新的定位代理方法
    -(void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
    {
         NSLog(@"经纬度值~~~%f---%f",location.coordinate.longitude,location.coordinate.latitude);
    }

    相关文章

      网友评论

      • eightzg:大赞
      • 驿路梨花处处开:你这个画片区的,会不会出现图片把图片放大在缩小,片区的透明度会变化,多试几次,会不会出现片区透明度变为0 的情况,看到了请回复,急,,,
      • Roy丶丶:刚好需要,万分感谢
      • PGOne爱吃饺子:大神 你好

      本文标题:高德地图

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