曹琴
摘?要:在進(jìn)行iOS開發(fā)時經(jīng)常會碰到需要使用地圖的問題,其中使用頻率較高的是百度地圖。本文就如何用百度地圖實(shí)現(xiàn)繪制軌跡的問題進(jìn)行了探討。
關(guān)鍵詞:iOS?Objective-C?百度地圖?繪制軌跡
中圖分類號:G202?文獻(xiàn)標(biāo)識碼:A?文章編號:1003-9082(2020)10-000-01
iOS項(xiàng)目需要使用百度地圖實(shí)現(xiàn)員工巡查功能,展示地圖,并顯示員工當(dāng)前所在位置。當(dāng)該員工點(diǎn)擊“開始巡查”按鈕,則追隨其腳步,進(jìn)行軌跡繪制,直到員工點(diǎn)擊“結(jié)束巡查”按鈕,完成軌跡的繪制,并截圖上傳至后臺。
路線繪制完畢的時候,不能截取到完整的“起點(diǎn)”+“終點(diǎn)”圖片,即使在截圖之前將地圖的centerCoordinate設(shè)置成中點(diǎn),然后設(shè)置2s延時后再截圖,也還是只能截取到以終點(diǎn)為中點(diǎn)的圖片。并不是設(shè)置center無效,而是地圖會閃一下中點(diǎn)的位置,然后還是回歸到終點(diǎn)的位置,最后才截圖。但是,當(dāng)?shù)貓D加載后,人為地拉動一下地圖,那么在截圖的時候,可以設(shè)置終點(diǎn)成功,并截取到完整的圖片。
解決辦法,如此設(shè)置:_mapView.showsUserLocation = NO。 這樣的話,就需要自己實(shí)現(xiàn)定位圖片的一些功能,譬如箭頭圖標(biāo);設(shè)備運(yùn)動方向變化的時候,箭頭要跟著指向前進(jìn)的方向;還有就是隨著定位的變化而變化位置。
以下是關(guān)鍵代碼:
#pragma mark - update location
-(void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error{?if(self.isStartTrace) {?//構(gòu)建分段顏色索引數(shù)組
BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coords count:2]; [self.mapView addOverlay:polyline];?[self.points addObject:location];?self.firstLocation = [self.userLocation copy];}}
#pragma mark - 繪制軌跡點(diǎn)
-(void)drawTrackWithPoints:(NSArray *)points{?CLLocationCoordinate2D coors[points.count];?NSInteger cnt = 0;?for (size_t i = 0; i < points.count; i++) { CLLocationCoordinate2D p = CLLocationCoordinate2DMake(((BMKLocation *)points[i]).location.coordinate.latitude, ((BMKLocation *)points[i]).location.coordinate.longitude);?coors[i] = p;?cnt++; }
BMKPolyline *line = [BMKPolyline polylineWithCoordinates:coors count:cnt];
BMKPointAnnotation *startAnnotation = [[BMKPointAnnotation alloc] init]; //起點(diǎn)annotation
BMKPointAnnotation *endAnnotation = [[BMKPointAnnotation alloc] init]; //終點(diǎn)annotation
dispatch_async(MAIN_QUEUE, ^{ [self.mapView addOverlay:line];?[self.mapView addAnnotation:startAnnotation]; [self.mapView addAnnotation:endAnnotation]; });}
#pragma mark - 設(shè)置起點(diǎn)、終點(diǎn)和當(dāng)前點(diǎn)樣式
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id
if ([annotation.title isEqualToString:kStartPositionTitle]) {
view = [mapView dequeueReusableAnnotationViewWithIdentifier: @"startAnnotationID"];
if (view == nil) { view = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:startViewID]; UILabel *lbl = [self createLabel:@"始"]; ?[view addSubview:lbl];}
}else if([annotation.title isEqualToString:kEndPositionTitle]){ UILabel *lbl = [self createLabel:@"終"];}else if([annotation.title isEqualToString:kArrowTitle]){ imageView.image = [UIImage imageNamed:@"sportArrow.png"]; return view;}
-(UILabel *)createLabel:(NSString *)text{
//label的frame x, y需要設(shè)置成-15,不然“始”會有一截沒有連線的空格
UILabel *lblStart = [[UILabel alloc] initWithFrame:CGRectMake(-15, -15, 30, 30)]; }
-(void)mapViewFitForCoordinates:(NSArray *)points{
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat+maxLat)*0.5, (minLon+maxLon)*0.5); span.latitudeDelta = 1.2 * ((maxLat-minLat)+0.01); ?span.longitudeDelta = 1.2 * ((maxLon - minLon)+0.01); }
結(jié)語
移動端使用地圖來定位、獲取軌跡等功能已經(jīng)成為人們生活中必不可少的一部分,極大地方便了人們進(jìn)行地理定位、導(dǎo)航等。就本文而言,有助于初次開發(fā)者在自己的iOS端APP中嵌入百度地圖,掌握它的繪制簡單軌跡的基本用法,從而為開發(fā)出與地圖相關(guān)的應(yīng)用打下基礎(chǔ)。
參考文獻(xiàn)
[1]劉春林,張翠翠.基于iOS的地圖類APP的開發(fā)應(yīng)用研究——以百度地圖為例[J].無線互聯(lián)科技,2018,15(23):46-47.
[2]林志偉,楊昱昺.基于Android系統(tǒng)的電子地圖運(yùn)動軌跡繪制的研究與實(shí)現(xiàn)[J].科技創(chuàng)新與應(yīng)用,2014(17):20-21.