iOS项目接入高德skd

前段时间做了个项目需要用到地图,虽然iOS有内置的地图API但是还是决定直接接入高德地图,实现标注视图的自定义。首先要按照文档接入高德sdk这没有什么好说的。
看代码。首先要创建地图

 [MAMapServices sharedServices].apiKey = @"5071a08ef9f79377e5a929362aef916e";
 _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, naviHeight+statrHeight+personView.frame.size.height, Swidth, Sheight-naviHeight+statrHeight+personView.frame.size.height)];
  _mapView.userTrackingMode = MAUserTrackingModeFollow;
[self.view addSubview:_mapView];

接下来是主要的创建标准视图,第一步要创建数据源:

- (void)creatAnnotaionModel:(S2CGamedetailret *)gameModel {
    if (gameModel != nil) {
        NSMutableArray *mutableArry = [NSMutableArray array];
      ...
            //标准视图创建
            MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
            CLLocationCoordinate2D coorDinate = CLLocationCoordinate2DMake([[gameModel.players[i] valueForKey:@"latitude" ] doubleValue],[[gameModel.players[i] valueForKey:@"longitude" ] doubleValue]);
            annotation.coordinate = coorDinate;
            annotation.title = @"d";
            [mutableArry addObject:annotation];
      ...
        [mutableArry addObject:annotation];
        //添加地图的标注视图数据源
        [_mapView addAnnotations:mutableArry];

        }
}

然后要正式创建标注视图了:

//这里是部分代码,函数返回的就是标注视图了。HeadAnnotationVeiw是MAAnnotationView的一个子类
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
    NSUInteger personRanking = 0;
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        static NSString *identy = @"annotation";
        //先从复用池中取视图
       HeadAnnotationVeiw   *annotationView = (HeadAnnotationVeiw *)[mapView dequeueReusableAnnotationViewWithIdentifier:identy];
        if (annotationView == nil) {
            annotationView = [[HeadAnnotationVeiw alloc] initWithAnnotation:annotation reuseIdentifier:identy];
        }
        //判断是否是登陆者的标注视图
      ...
            annotationView.faImage.imageURL = [NSURL URLWithString:detail.loginUserInfo.headImage];
            //annotationView.selected = YES; //默认登陆者是处于选择状态的
            annotationView.imageName = @"map_hover_bg";
            annotationView.isPK = detail.loginUserInfo.pkIng;
            annotationView.personId = detail.loginUserInfo.id;
           ....
        }

        return annotationView;
    }
    return nil;
}

HeadAnnotationVeiw继承自MAAnnotationView,有个初始化方法最好实现一下:

 */
- (id)initWithAnnotation:(id <MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;

选中标注视图

//选中标注视图
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
    if ([view isKindOfClass:[HeadAnnotationVeiw class]]) {
        headView = (HeadAnnotationVeiw *)view;
        point = [headView.superview convertPoint:headView.frame.origin toView:self.view]; //获取标注视图在屏幕中的位置
      //区分选中的视图可以使用经纬度
    }
}

添加标注视图会调用这个方法

- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    [_mapView showAnnotations:_mapView.annotations animated:YES];
}

更新位置信息

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    static NSInteger i = 0;
    if (i >= 1) {
        return;
    }
    i ++;
    CLLocationCoordinate2D coorDinate = userLocation.coordinate;
    NSString *longitude = [NSString stringWithFormat:@"%f",coorDinate.longitude];
    NSString *latitude = [NSString stringWithFormat:@"%f",coorDinate.latitude];
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值