美文网首页iOS-Objective-CiOS-地图iOS Developer
自定义高德地图定位小蓝点(用户位置精度圈)

自定义高德地图定位小蓝点(用户位置精度圈)

作者: 追沐 | 来源:发表于2017-06-15 20:54 被阅读400次

开发中有些会遇到地图的使用,对于高德地图当前位置的小蓝点,根据不同的UI效果可能需要实现不同效果,比如讲小蓝点换成用户头像、网络头像等。这里多高德地图的当前位置“小蓝点”进行处理,使得该功能更加实用,扩展性更强。

思路:

前提:已经集成了高德地图SDK,并且设置了AppKey,添加了定位权限相关设置,此处参照高德地图官方文档。

1、首先我们往视图上添加一个地图

- (void)creatMapView {
    _mapView = [[MAMapView alloc]initWithFrame:self.view.bounds];
    _mapView.delegate = self;
    _mapView.showsCompass = NO;
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    _mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
    [self.view addSubview:_mapView];
}

一些基础属性的设置这里不多说,主要说明一下,如果需要高德地图显示定位信息,需要将showUserLocation这个属性设置成YES,这个属性设置成YES后,用户定位信息(包括位置与设备方向等数据)改变,以下代理方法就会执行

#pragma mark MAMapViewDelegate
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    
    NSLog(@"%d",updatingLocation);
    NSLog(@"位置更新");
    NSLog(@"当前位置:%f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
}

从此回调方法中我们可以时时的获取到用户当前的经纬度信息。

2、显示地图定位的“小蓝点”,自定义小蓝点

以上我们已经将_mapView.showsUserLocation设置成了YES,如果需要定位追踪,可以将属性userTrackingMode设置成MAUserTrackingModeFollow,该属性是个枚举。

//如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
_mapView.showsUserLocation = YES;
_mapView.userTrackingMode = MAUserTrackingModeFollow;

要自定义小蓝点,需要将属性值customizeUserLocationAccuracyCircleRepresentation设置成YES

//是否自定义用户位置精度圈
_mapView.customizeUserLocationAccuracyCircleRepresentation = YES;

2.1 高德SDK提供了类MAUserLocationRepresentation来修改小蓝点的外观等

//注:以下代码全部摘抄自高德地图开放平台
MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
r.showsAccuracyRing = NO;///精度圈是否显示,默认YES
r.showsHeadingIndicator = NO;///是否显示方向指示(MAUserTrackingModeFollowWithHeading模式开启)。默认为YES
r.fillColor = [UIColor redColor];///精度圈 填充颜色, 默认 kAccuracyCircleDefaultColor
r.strokeColor = [UIColor blueColor];///精度圈 边线颜色, 默认 kAccuracyCircleDefaultColor
r.lineWidth = 2;///精度圈 边线宽度,默认0
r.enablePulseAnnimation = NO;///内部蓝色圆点是否使用律动效果, 默认YES
r.locationDotBgColor = [UIColor greenColor];///定位点背景色,不设置默认白色
r.locationDotFillColor = [UIColor grayColor];///定位点蓝色圆点颜色,不设置默认蓝色
r.image = [UIImage imageNamed:@"你的图片"]; ///定位图标, 与蓝色原点互斥

以上设置后还需要调用一个方法

[self.mapView updateUserLocationRepresentation:r];

以上方式可简单的实现修改小蓝点,但是可扩展性不强。

2.2 自己设置高德额地图的小蓝点

不通过MAUserLocationRepresentation设置但前位置。

///是否自定义用户位置精度圈(userLocationAccuracyCircle)对应的 view, 默认为 NO.\n 如果为YES: 会调用 - (MAOverlayRenderer *)mapView (MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay 若返回nil, 则不加载.\n 如果为NO : 会使用默认的样式.
//是否自定义用户位置精度圈
_mapView.customizeUserLocationAccuracyCircleRepresentation = YES;

以上是高德Api里的描述,也就是说将customizeUserLocationAccuracyCircleRepresentation设置成YES,可以自己通过方法定义小蓝点。

当前位置的实质也是一个大头针,那么我们在添加大头针的方法里面设置

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
 
    //用户当前位置大头针
    if ([annotation isKindOfClass:[MAUserLocation class]])
    {
        static NSString *userLocationStyleReuseIndetifier = @"userLocationStyleReuseIndetifier";
        
        MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:userLocationStyleReuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:userLocationStyleReuseIndetifier];
        }
        
        annotationView.canShowCallout = NO;
        annotationView.image = [UIImage imageNamed:@"heardImg"];
        annotationView.frame = CGRectMake(0, 0, 26, 26);
        annotationView.contentMode = UIViewContentModeScaleToFill;
        annotationView.layer.masksToBounds = YES;
        
        return annotationView;
    }
    
    //其他大头针设置
    else if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        static NSString *locationBackViewReuseIndetifier = @"locationBackViewReuseIndetifier";
        
        MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:locationBackViewReuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:locationBackViewReuseIndetifier];
        }

        annotationView.canShowCallout = NO;
        annotationView.image = [UIImage imageNamed:@"heardImg_default"];
        annotationView.frame = CGRectMake(0, 0, 10, 10);
        annotationView.contentMode = UIViewContentModeScaleToFill;
        annotationView.layer.masksToBounds = YES;
    }
    
    return nil;
}

以上方法,只要往地图上添加大头针就会通过此方法生成大头针的view,当前位置也是个大头针,通过判断如果是当前位置的大头针,我们可以自己定义。另外通过以下方法自定义小蓝点。

- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay {
    //自定义经度对应的MACircleView
    if (overlay == mapView.userLocationAccuracyCircle) {
        
        MACircleRenderer *accuracyCircleRenderer = [[MACircleRenderer alloc] initWithCircle:overlay];
        
        accuracyCircleRenderer.lineWidth    = 2.f;
        accuracyCircleRenderer.strokeColor  = [UIColor lightGrayColor];
        accuracyCircleRenderer.fillColor    = [UIColor colorWithRed:1 green:0 blue:0 alpha:.3];
        
        return accuracyCircleRenderer;
    }
    return nil;
}

github地址:
https://github.com/Mexiang/GDMapUserLocation

相关文章

网友评论

  • 缪雨轩:2D的确实没有customizeUserLocationAccuracyCircleRepresentation这个属性。。
  • 程序亦非猿580230:怎么设置小蓝点不可点击
    追沐:@程序亦非猿580230 iOS的。
    程序亦非猿580230:@追沐 这是android的吗?
    追沐:两个方法

    方法一:设置小蓝点的annotationView不可点击
    annotationView.enabled = NO;

    方法二:在实现大头针点击方法中不实现功能代码也可以
    - (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
    //点击小蓝点大头针的代理事件
    if ([view.annotation isKindOfClass:[MAUserLocation class]]) {
    NSLog(@"点击小蓝点");
    }
    //其他大头针点击代理
    else {

    }
    }
  • 咔客:_mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
    这属性设置 好像不识别啊?
    咔客:@追沐 rotationDegree 没有找到 直接报错
    咔客:@追沐 设置了啊
    追沐:_mapView.showsUserLocation = YES;
    showsUserLocation属性设置了吗。
  • 翻滚的炒勺2013:_mapView.customizeUserLocationAccuracyCircleRepresentation 没有这个属性
    翻滚的炒勺2013:@追沐 我的版本是 Using AMap2DMap (4.6.0) 确实没有这个属性
    翻滚的炒勺2013:@追沐 是2d吗
    追沐:你换一下SDK,最新的SDK里面有这个属性的,在这里:

    @interface MAMapView (UserLocation)

    ///是否显示用户位置
    @property (nonatomic) BOOL showsUserLocation;

    ///当前的位置数据
    @property (nonatomic, readonly) MAUserLocation *userLocation;

    ///是否自定义用户位置精度圈(userLocationAccuracyCircle)对应的 view, 默认为 NO.\n 如果为YES: 会调用 - (MAOverlayRenderer *)mapView (MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay 若返回nil, 则不加载.\n 如果为NO : 会使用默认的样式.
    @property (nonatomic) BOOL customizeUserLocationAccuracyCircleRepresentation;

    ///用户位置精度圈 对应的overlay
    @property (nonatomic, readonly) MACircle *userLocationAccuracyCircle;
  • 刘超_a594:兄弟你这个只是简单的换了个图片吧 ,最重要的一点是要做到图片跟随方向转动
    追沐:@hukun 我没明白你的问题,是缩放地图,蓝点会偏移?
    hukun:自定义蓝点后,从最大放缩比例缩小到很大的时候,这个小蓝点会偏移,没找到刷新位置的方法,你有什么办法吗
    追沐:- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
    {
    NSLog(@"%f,%f,%f",userLocation.heading.x,userLocation.heading.y,userLocation.heading.z);

    MAAnnotationView *hearderAnnotationView = [self.mapView viewWithTag:kUserHearderAnnotaionViewTag];
    if (hearderAnnotationView)
    {
    hearderAnnotationView.transform = CGAffineTransformIdentity;
    CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI*userLocation.heading.magneticHeading/180.0);
    hearderAnnotationView.transform = transform;
    }
    }

    这就简单的实现了。

本文标题:自定义高德地图定位小蓝点(用户位置精度圈)

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