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

这篇博客介绍了如何在iOS应用中使用高德地图展示多个带有自定义图片的大头针,并实现点击效果。作者跳过了导入和基础设置步骤,直接讲解了创建一个继承自MAPointAnnotation的类,添加图片属性,然后在viewController中模拟数据,添加4个标注到地图上。同时,遵循MAMapViewDelegate并实现相关代理方法来完善点击交互功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

导入等过程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);
        }
    }

}












评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值