前段時間做了個項目需要用到地圖,雖然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];
}