美文网首页
高德地图点击不同大头针显示不同图片

高德地图点击不同大头针显示不同图片

作者: developer宋小宇 | 来源:发表于2017-11-30 12:13 被阅读0次
IMG_0467.PNG

问题:气泡上显示乘客的头像,怎样获取到每个大头针对应哪个头像
解决:可以继承MAPointAnnotation, 类似下面代码
@interface YourPointAnnotation : MAPointAnnotation
@property UIImageView* yourImageView;
@end
调用addAnnotation:时添加的类型改为YourPointAnnotation

上代码:

pragma mark - 打点(附近的乘客和司机)

  • (void)addPersonAnnotation{
    NSArray *personArr = self.personsLocationDict[@"persons"];
    NSInteger count = personArr.count;
    for (NSInteger i=0; i<count; i++) {
    NSDictionary *personDict = personArr[i];
    NSArray *locationArr = personDict[@"gaodeLocation"];
    QCXPointAnnotation *pointAnnotation = [[QCXPointAnnotation alloc] init];
    pointAnnotation.coordinate = CLLocationCoordinate2DMake([locationArr[1] floatValue], [locationArr[0] floatValue]);
    pointAnnotation.title = personDict[@"name"];
    pointAnnotation.subtitle = personDict[@"nickName"];

      UIImageView *headerImgView = [[UIImageView alloc] init];
      headerImgView.frame =CGRectMake(0, 0, 50, 50);
      [headerImgView sd_setImageWithURL:personDict[@"picture"] placeholderImage:[UIImage imageNamed:@"car"]];
      pointAnnotation.headImageView = headerImgView;
      [self.annotationsArr addObject:pointAnnotation];
    

    }

    [self.mapView addAnnotations:self.annotationsArr];
    [self.mapView showAnnotations:self.annotationsArr animated:YES];
    }

代理方法:

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

    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
    static NSString *reuseIndetifier = @"annotationReuseIndetifier";
    MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
    if (annotationView == nil)
    {
    annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
    }

      annotationView.canShowCallout               = YES;
      annotationView.draggable                    = YES;
      annotationView.image = [UIImage imageNamed:@"car"];
      
      QCXPointAnnotation *qcxAnn = annotation;
      annotationView.leftCalloutAccessoryView = qcxAnn.headImageView;
      return annotationView;
    

    }
    return nil;
    }

结束

相关文章

网友评论

      本文标题:高德地图点击不同大头针显示不同图片

      本文链接:https://www.haomeiwen.com/subject/zrkvbxtx.html