曹琴
摘?要:在進行iOS開發時經常會碰到需要使用地圖的問題,其中使用頻率較高的是百度地圖。本文就如何用百度地圖實現繪制軌跡的問題進行了探討。
關鍵詞:iOS?Objective-C?百度地圖?繪制軌跡
中圖分類號:G202?文獻標識碼:A?文章編號:1003-9082(2020)10-000-01
iOS項目需要使用百度地圖實現員工巡查功能,展示地圖,并顯示員工當前所在位置。當該員工點擊“開始巡查”按鈕,則追隨其腳步,進行軌跡繪制,直到員工點擊“結束巡查”按鈕,完成軌跡的繪制,并截圖上傳至后臺。
路線繪制完畢的時候,不能截取到完整的“起點”+“終點”圖片,即使在截圖之前將地圖的centerCoordinate設置成中點,然后設置2s延時后再截圖,也還是只能截取到以終點為中點的圖片。并不是設置center無效,而是地圖會閃一下中點的位置,然后還是回歸到終點的位置,最后才截圖。但是,當地圖加載后,人為地拉動一下地圖,那么在截圖的時候,可以設置終點成功,并截取到完整的圖片。
解決辦法,如此設置:_mapView.showsUserLocation = NO。 這樣的話,就需要自己實現定位圖片的一些功能,譬如箭頭圖標;設備運動方向變化的時候,箭頭要跟著指向前進的方向;還有就是隨著定位的變化而變化位置。
以下是關鍵代碼:
#pragma mark - update location
-(void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error{?if(self.isStartTrace) {?//構建分段顏色索引數組
BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coords count:2]; [self.mapView addOverlay:polyline];?[self.points addObject:location];?self.firstLocation = [self.userLocation copy];}}
#pragma mark - 繪制軌跡點
-(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]; //起點annotation
BMKPointAnnotation *endAnnotation = [[BMKPointAnnotation alloc] init]; //終點annotation
dispatch_async(MAIN_QUEUE, ^{ [self.mapView addOverlay:line];?[self.mapView addAnnotation:startAnnotation]; [self.mapView addAnnotation:endAnnotation]; });}
#pragma mark - 設置起點、終點和當前點樣式
-(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需要設置成-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); }
結語
移動端使用地圖來定位、獲取軌跡等功能已經成為人們生活中必不可少的一部分,極大地方便了人們進行地理定位、導航等。就本文而言,有助于初次開發者在自己的iOS端APP中嵌入百度地圖,掌握它的繪制簡單軌跡的基本用法,從而為開發出與地圖相關的應用打下基礎。
參考文獻
[1]劉春林,張翠翠.基于iOS的地圖類APP的開發應用研究——以百度地圖為例[J].無線互聯科技,2018,15(23):46-47.
[2]林志偉,楊昱昺.基于Android系統的電子地圖運動軌跡繪制的研究與實現[J].科技創新與應用,2014(17):20-21.