開發(fā)中有些會(huì)遇到地圖的使用,對(duì)于高德地圖當(dāng)前位置的小藍(lán)點(diǎn),根據(jù)不同的UI效果可能需要實(shí)現(xiàn)不同效果,比如講小藍(lán)點(diǎn)換成用戶頭像、網(wǎng)絡(luò)頭像等。這里多高德地圖的當(dāng)前位置“小藍(lán)點(diǎn)”進(jìn)行處理,使得該功能更加實(shí)用,擴(kuò)展性更強(qiáng)。
思路:
前提:已經(jīng)集成了高德地圖SDK,并且設(shè)置了AppKey,添加了定位權(quán)限相關(guān)設(shè)置,此處參照高德地圖官方文檔。
1、首先我們往視圖上添加一個(gè)地圖
- (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這個(gè)屬性設(shè)置成YES,這個(gè)屬性設(shè)置成YES后,用戶定位信息(包括位置與設(shè)備方向等數(shù)據(jù))改變,以下代理方法就會(huì)執(zhí)行
#pragma mark MAMapViewDelegate
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
NSLog(@"%d",updatingLocation);
NSLog(@"位置更新");
NSLog(@"當(dāng)前位置:%f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
}
從此回調(diào)方法中我們可以時(shí)時(shí)的獲取到用戶當(dāng)前的經(jīng)緯度信息。
2、顯示地圖定位的“小藍(lán)點(diǎn)”,自定義小藍(lán)點(diǎn)
以上我們已經(jīng)將_mapView.showsUserLocation設(shè)置成了YES,如果需要定位追蹤,可以將屬性u(píng)serTrackingMode設(shè)置成MAUserTrackingModeFollow,該屬性是個(gè)枚舉。
//如果您需要進(jìn)入地圖就顯示定位小藍(lán)點(diǎn),則需要下面兩行代碼
_mapView.showsUserLocation = YES;
_mapView.userTrackingMode = MAUserTrackingModeFollow;
要自定義小藍(lán)點(diǎn),需要將屬性值customizeUserLocationAccuracyCircleRepresentation設(shè)置成YES
//是否自定義用戶位置精度圈
_mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
2.1 高德SDK提供了類MAUserLocationRepresentation來修改小藍(lán)點(diǎn)的外觀等
//注:以下代碼全部摘抄自高德地圖開放平臺(tái)
MAUserLocationRepresentation *r = [[MAUserLocationRepresentation alloc] init];
r.showsAccuracyRing = NO;///精度圈是否顯示,默認(rèn)YES
r.showsHeadingIndicator = NO;///是否顯示方向指示(MAUserTrackingModeFollowWithHeading模式開啟)。默認(rèn)為YES
r.fillColor = [UIColor redColor];///精度圈 填充顏色, 默認(rèn) kAccuracyCircleDefaultColor
r.strokeColor = [UIColor blueColor];///精度圈 邊線顏色, 默認(rèn) kAccuracyCircleDefaultColor
r.lineWidth = 2;///精度圈 邊線寬度,默認(rèn)0
r.enablePulseAnnimation = NO;///內(nèi)部藍(lán)色圓點(diǎn)是否使用律動(dòng)效果, 默認(rèn)YES
r.locationDotBgColor = [UIColor greenColor];///定位點(diǎn)背景色,不設(shè)置默認(rèn)白色
r.locationDotFillColor = [UIColor grayColor];///定位點(diǎn)藍(lán)色圓點(diǎn)顏色,不設(shè)置默認(rèn)藍(lán)色
r.image = [UIImage imageNamed:@"你的圖片"]; ///定位圖標(biāo), 與藍(lán)色原點(diǎn)互斥
以上設(shè)置后還需要調(diào)用一個(gè)方法
[self.mapView updateUserLocationRepresentation:r];
以上方式可簡(jiǎn)單的實(shí)現(xiàn)修改小藍(lán)點(diǎn),但是可擴(kuò)展性不強(qiáng)。
2.2 自己設(shè)置高德額地圖的小藍(lán)點(diǎn)
不通過MAUserLocationRepresentation設(shè)置但前位置。
///是否自定義用戶位置精度圈(userLocationAccuracyCircle)對(duì)應(yīng)的 view, 默認(rèn)為 NO.\n 如果為YES: 會(huì)調(diào)用 - (MAOverlayRenderer *)mapView (MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay 若返回nil, 則不加載.\n 如果為NO : 會(huì)使用默認(rèn)的樣式.
//是否自定義用戶位置精度圈
_mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
以上是高德Api里的描述,也就是說將customizeUserLocationAccuracyCircleRepresentation設(shè)置成YES,可以自己通過方法定義小藍(lán)點(diǎn)。
當(dāng)前位置的實(shí)質(zhì)也是一個(gè)大頭針,那么我們?cè)谔砑哟箢^針的方法里面設(shè)置
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
//用戶當(dāng)前位置大頭針
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;
}
以上方法,只要往地圖上添加大頭針就會(huì)通過此方法生成大頭針的view,當(dāng)前位置也是個(gè)大頭針,通過判斷如果是當(dāng)前位置的大頭針,我們可以自己定義。另外通過以下方法自定義小藍(lán)點(diǎn)。
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay {
//自定義經(jīng)度對(duì)應(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;
}