美文网首页
高德地图MAMapKit使用心得随笔

高德地图MAMapKit使用心得随笔

作者: 橡树花香 | 来源:发表于2018-06-29 09:48 被阅读0次

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(20
    kRating, 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);//标注点的经纬度
    self.annotation = anno;
    //添加标注点至地图
    [self.mapView addAnnotation:self.annotation];
    //显示标注点(会自动缩放地图已到达显示所有需要显示的标注点,若不添加该句,地图显示为设置的缩放的等级)
    [self.mapView showAnnotations:@[self.annotation] animated:YES];
    4、设置经纬度数组(根据数组数动态生成CLLocationCoordinate2D结构体数组的内存空间个数)
    CLLocationCoordinate2D *coords = (CLLocationCoordinate2D *)malloc([array count] * sizeof(CLLocationCoordinate2D));
    5、地图状态设置(可让地图根据某个角度动态的移动地图)
    [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并传递相应的数值
/*

  • 轨迹纠偏
    */
  • (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;
    }

相关文章

网友评论

      本文标题:高德地图MAMapKit使用心得随笔

      本文链接:https://www.haomeiwen.com/subject/kevoyftx.html