您的位置:首页 > 移动开发 > IOS开发

iOS项目接入高德skd

2016-02-18 16:47 344 查看
前段时间做了个项目需要用到地图,虽然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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: