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

iOS高德地图 多个大头针显示图片 点击效果

2016-11-17 17:40 686 查看
最近做一款有关地图的软件  使用到了高德地图  将一些经验记录下来 第一篇博客  以后会陆续更新 希望看到的朋友多多支持啦。

导入等过程PASS掉。

新建一个类  继承 MAPointAnnotation  添加图片属性

@property (nonatomic,copy)NSString *imageString;


在viewController.m 中添加模拟数据

self.annotations = [NSMutableArrayarray];
CLLocationCoordinate2D coordinates[10] = {
{31.34201706,104.19923639},
{31.34131706,104.19943639},
{31.34241706,104.19973639},
{31.34111706,104.20093639}};
for (int i =0; i <4; ++i)
{
ZSPointAnnotation *a1 = [[ZSPointAnnotationalloc]init];
a1.coordinate = coordinates[i];
a1.title = [NSStringstringWithFormat:@"标题: %d", i];
a1.imageString = [NSStringstringWithFormat:@"图%d.jpg",i];
[self.annotationsaddObject:a1];
}

模拟4条数据

创建地图添加annotations  遵循代理MAMapViewDelegate

self.mapView.delegate =self;
[self.mapViewaddAnnotations:self.annotations];
[self.mapViewshowAnnotations:self.annotationsedgePadding:UIEdgeInsetsMake(20,20,20,80)animated:YES];

实现代理方法

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{

customAnnotationView参考高德demo中 自定义样式标注
staticNSString *customReuseIndetifier =@"customReuseIndetifier";
CustomAnnotationView *annotationView = (CustomAnnotationView*)[mapViewdequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];
if (annotationView ==nil)
{
annotationView = [[CustomAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:customReuseIndetifier];

annotationView.canShowCallout =NO;
annotationView.draggable =YES;
annotationView.calloutOffset =CGPointMake(0, -5);
}

这里我们遍历数组 拿到point 与地图上annotation上title比较 相同的话 设置图片为模拟数据的image
for (ZSPointAnnotation *pointinself.annotations) {
if ([[annotationtitle]isEqualToString:point.title]) {
annotationView.portraitImageView.image = [UIImageimageNamed:point.imageString];
annotationView.portraitImageView.layer.cornerRadius =25;
annotationView.portraitImageView.layer.masksToBounds =YES;
}
}

return annotationView;

}

接下来实现它的点击方法  方法相类似

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {

NSArray * array = [NSArrayarrayWithArray:_mapView.annotations];
这里遍历mapView上的annotations 比较经度(纬度)title 都可以 这样就可以啦
for (ZSPointAnnotation *pointin array) {
if (view.annotation.coordinate.latitude == point.coordinate.latitude){
NSLog(@"点了我 %@",point.title);
NSLog(@"图片是我 %@",point.imageString);
}else{
NSLog(@"没点我 %@",point.title);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息