自定義高德地圖定位小藍點(用戶位置精度圈)

開發(fā)中有些會遇到地圖的使用,對于高德地圖當前位置的小藍點,根據(jù)不同的UI效果可能需要實現(xiàn)不同效果,比如講小藍點換成用戶頭像、網(wǎng)絡(luò)頭像等。這里多高德地圖的當前位置“小藍點”進行處理,使得該功能更加實用,擴展性更強。

思路:

前提:已經(jīng)集成了高德地圖SDK,并且設(shè)置了AppKey,添加了定位權(quán)限相關(guān)設(shè)置,此處參照高德地圖官方文檔。

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];
}

一些基礎(chǔ)屬性的設(shè)置這里不多說,主要說明一下,如果需要高德地圖顯示定位信息,需要將showUserLocation這個屬性設(shè)置成YES,這個屬性設(shè)置成YES后,用戶定位信息(包括位置與設(shè)備方向等數(shù)據(jù))改變,以下代理方法就會執(zhí)行

#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);
}

從此回調(diào)方法中我們可以時時的獲取到用戶當前的經(jīng)緯度信息。

2、顯示地圖定位的“小藍點”,自定義小藍點

以上我們已經(jīng)將_mapView.showsUserLocation設(shè)置成了YES,如果需要定位追蹤,可以將屬性userTrackingMode設(shè)置成MAUserTrackingModeFollow,該屬性是個枚舉。

//如果您需要進入地圖就顯示定位小藍點,則需要下面兩行代碼
_mapView.showsUserLocation = YES;
_mapView.userTrackingMode = MAUserTrackingModeFollow;

要自定義小藍點,需要將屬性值customizeUserLocationAccuracyCircleRepresentation設(shè)置成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;///內(nèi)部藍色圓點是否使用律動效果, 默認YES
r.locationDotBgColor = [UIColor greenColor];///定位點背景色,不設(shè)置默認白色
r.locationDotFillColor = [UIColor grayColor];///定位點藍色圓點顏色,不設(shè)置默認藍色
r.image = [UIImage imageNamed:@"你的圖片"]; ///定位圖標, 與藍色原點互斥

以上設(shè)置后還需要調(diào)用一個方法

[self.mapView updateUserLocationRepresentation:r];

以上方式可簡單的實現(xiàn)修改小藍點,但是可擴展性不強。

2.2 自己設(shè)置高德額地圖的小藍點

不通過MAUserLocationRepresentation設(shè)置但前位置。

///是否自定義用戶位置精度圈(userLocationAccuracyCircle)對應(yīng)的 view, 默認為 NO.\n 如果為YES: 會調(diào)用 - (MAOverlayRenderer *)mapView (MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay 若返回nil, 則不加載.\n 如果為NO : 會使用默認的樣式.
//是否自定義用戶位置精度圈
_mapView.customizeUserLocationAccuracyCircleRepresentation = YES;

以上是高德Api里的描述,也就是說將customizeUserLocationAccuracyCircleRepresentation設(shè)置成YES,可以自己通過方法定義小藍點。

當前位置的實質(zhì)也是一個大頭針,那么我們在添加大頭針的方法里面設(shè)置

- (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;
    }
    
    //其他大頭針設(shè)置
    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 {
    //自定義經(jīng)度對應(yīng)的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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容