1、注冊獲取應用KEY(http://lbs.amap.com/api/ios-sdk/guide/create-project/get-key)
2德迹、添加MAMapView至需要的頁面中
pragma mark - init
- (MAMapView )mapView
{
if (!_mapView) {
_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
_mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_mapView.delegate = self;
_mapView.showsScale = NO; //不顯示比例尺
// _mapView.showsCompass = NO; //關閉指南針
_mapView.compassOrigin = CGPointMake(20kRating, 20*kRating);//指南針的左上角坐標點位置
_mapView.centerCoordinate = CLLocationCoordinate2DMake(22.57, 113.90);//設置地圖中心點
_mapView.zoomLevel = 3;
_mapView.minZoomLevel = 3;
_mapView.rotateCameraEnabled = NO;
//取出logo圖標
[_mapView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[UIImageView class]]) {
UIImageView * logoM = obj;
logoM.layer.contents = (__bridge id)[UIImage imageNamed:@""].CGImage;
}
}];
}
return _mapView;
}
//在viewDidLoad中添加
[self.view addSubview:self.mapView];
3闹蒜、設置定點標注信息
MAAnimatedAnnotation *anno = [[MAAnimatedAnnotation alloc] init];
anno.coordinate = CLLocationCoordinate2DMake(positionModel.latitude, positionModel.longitude);//標注點的經(jīng)緯度
self.annotation = anno;
//添加標注點至地圖
[self.mapView addAnnotation:self.annotation];
//顯示標注點(會自動縮放地圖已到達顯示所有需要顯示的標注點戚哎,若不添加該句,地圖顯示為設置的縮放的等級)
[self.mapView showAnnotations:@[self.annotation] animated:YES];
4嫂用、設置經(jīng)緯度數(shù)組(根據(jù)數(shù)組數(shù)動態(tài)生成CLLocationCoordinate2D結構體數(shù)組的內存空間個數(shù))
CLLocationCoordinate2D *coords = (CLLocationCoordinate2D *)malloc([array count] * sizeof(CLLocationCoordinate2D));
5型凳、地圖狀態(tài)設置(可讓地圖根據(jù)某個角度動態(tài)的移動地圖)
[self.mapView setMapStatus:[MAMapStatus statusWithCenterCoordinate:coors[1]
zoomLevel:18
rotationDegree:(360-self.vehicleModel.vehicle.direction)
cameraDegree:0
screenAnchor:CGPointMake(0.5, 0.5)]
animated:YES
duration:10];
6、在需要的地方調取configTrace并傳遞相應的數(shù)值
/*
- 軌跡糾偏
*/
-
(NSArray *)positionTransformTraceLocation
{
NSMutableArray *array = [NSMutableArray array];
if (self.historyModel.positions.count > 0) {
for (ZJPositionModel *positionModel in self.historyModel.positions) {
MATraceLocation *location = [[MATraceLocation alloc] init];
location.loc = CLLocationCoordinate2DMake(positionModel.latitude, positionModel.longitude);
location.speed = positionModel.speed;
location.time = positionModel.gpsTime;
location.angle = positionModel.direction;[array addObject:location]; }
}
return array;
} -
(void)configTrace
{
if (self.historyModel.positions.count == 1) {//只有一個點不糾正軌跡
if (self.processedOverlays && self.processedOverlays.count > 0) {
[self.mapView removeOverlays:self.processedOverlays];
[self.processedOverlays removeAllObjects];
}} else {
__weak typeof(self) weakSelf = self;MATraceManager *manager = [[MATraceManager alloc] init]; NSOperation *operation = [manager queryProcessedTraceWith:[self positionTransformTraceLocation] type:-1 processingCallback:^(int index, NSArray<MATracePoint *> *points) { [weakSelf addSubTrace:points toMapView:weakSelf.mapView]; } finishCallback:^(NSArray<MATracePoint *> *points, double distance) { weakSelf.queryOperation = nil; [weakSelf addFullTrace:points toMapView:weakSelf.mapView]; } failedCallback:^(int errorCode, NSString *errorDesc) { NSLog(@"Error: %@", errorDesc); weakSelf.queryOperation = nil; }]; self.queryOperation = operation;
}
}
-
(MAMultiPolyline )makePolyLineWith:(NSArray<MATracePoint> *)tracePoints {
if(tracePoints.count == 0) {
return nil;
}CLLocationCoordinate2D *pCoords = malloc(sizeof(CLLocationCoordinate2D) * tracePoints.count);
if(!pCoords) {
return nil;
}for(int i = 0; i < tracePoints.count; ++i) {
MATracePoint *p = [tracePoints objectAtIndex:i];
CLLocationCoordinate2D *pCur = pCoords + i;
pCur->latitude = p.latitude;
pCur->longitude = p.longitude;
}MAMultiPolyline *polyline = [MAMultiPolyline polylineWithCoordinates:pCoords count:tracePoints.count drawStyleIndexes:@[@10, @60]];
if(pCoords) {
free(pCoords);
}
return polyline;
} -
(void)addFullTrace:(NSArray<MATracePoint*> *)tracePoints toMapView:(MAMapView *)mapView{
MAMultiPolyline *polyline = [self makePolyLineWith:tracePoints];
if(!polyline) {
return;
}
[mapView removeOverlays:self.processedOverlays];
[self.processedOverlays removeAllObjects];[mapView setVisibleMapRect:MAMapRectInset(polyline.boundingMapRect, -1000, -1000)];
[self.processedOverlays addObject:polyline];
[mapView addOverlays:self.processedOverlays];
[mapView showOverlays:self.processedOverlays animated:YES];
} -
(void)addSubTrace:(NSArray<MATracePoint*> *)tracePoints toMapView:(MAMapView *)mapView {
MAMultiPolyline *polyline = [self makePolyLineWith:tracePoints];
if(!polyline) {
return;
}MAMapRect visibleRect = [mapView visibleMapRect];
if(!MAMapRectContainsRect(visibleRect, polyline.boundingMapRect)) {
MAMapRect newRect = MAMapRectUnion(visibleRect, polyline.boundingMapRect);
[mapView setVisibleMapRect:newRect];
}[self.processedOverlays addObject:polyline];
[mapView addOverlay:polyline];
[mapView showOverlays:@[polyline] animated:YES];
}
pragma mark - mapview delegate
-
(MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
if ([overlay isKindOfClass:[MAMultiPolyline class]])
{
MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:(MAMultiPolyline *)overlay];
polylineRenderer.lineWidth = 16.f;
polylineRenderer.strokeImage = [UIImage imageNamed:@"car_trace"];return polylineRenderer;
}
return nil;
}