【问题标题】:MKMapViewAnnotation Image from custom image different for many pins来自自定义图像的 MKMapView 注释图像对于许多引脚不同
【发布时间】:2014-10-17 11:19:04
【问题描述】:

我正在尝试从位置数据库创建许多引脚,每个引脚都有自定义图像。图片以 base64 格式从服务器传递,我可以从 base64 数据创建UIImage,但我无法将图片作为图标而不是每个注释的 pin 上传。

有什么想法可以做到这一点吗?

我尝试关注这些 SO 帖子以提供帮助,但他们只能创建一个预设,我不知道如何编辑所有这些帖子,因此它们每个都有一个独特的图标:

SO link 1SO link 2

有什么想法吗?

这是我创建图钉/注释的代码,以防有人感兴趣:

func mapTheseCoordinates(lat: Double, lng: Double, username: NSString, fullname: NSString) {
    let location = CLLocationCoordinate2D(
        latitude: lat,
        longitude: lng
    )
    // 2
    let span = MKCoordinateSpanMake(0.15, 0.15)
    let region = MKCoordinateRegion(center: location, span: span)
    mapView.setRegion(region, animated: true)

    //3
    let annotation = MKPointAnnotation()
    annotation.setCoordinate(location)
    annotation.title = username
    annotation.subtitle = fullname
    mapView.addAnnotation(annotation)
}

【问题讨论】:

标签: ios swift mkmapview


【解决方案1】:

你可以使用mkmapview的这个Delegate事件,这里你可以根据你的注解改变不同的图像

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
    //MyPin.pinColor = MKPinAnnotationColorPurple;
    MyPin.highlighted = NO;
    MyPin.image = [UIImage imageNamed:@"pin_red"];

    return MyPin;
}

每次添加注释时都会调用此委托事件,但不要忘记为 mapview 设置委托

[mapLocation addAnnotation:annotObj]; 

【讨论】:

  • 我怎样才能快速做到这一点?我确实将帖子标记为 swift,并提供了 swift 代码,但我不太了解 Objective C。
  • 我不知道 swift 但我想它应该可以工作stackoverflow.com/questions/25631410/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多