自定義高德地圖定位小藍(lán)點(diǎn)(用戶位置精度圈)

開發(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;
}

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,836評(píng)論 6 540
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,275評(píng)論 3 428
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,904評(píng)論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,633評(píng)論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,368評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,736評(píng)論 1 328
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,740評(píng)論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,919評(píng)論 0 289
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,481評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,235評(píng)論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,427評(píng)論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,968評(píng)論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,656評(píng)論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,055評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,348評(píng)論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,160評(píng)論 3 398
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,380評(píng)論 2 379

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