ios - Mapbox Annotation 图像居中

标签 ios objective-c mapbox

我开始在一个项目中使用 MapBox。当我使用自定义图像添加注释时,它居中不正确。它看起来比原始引脚低一点。 是否可以更改标注图像的中心点?

它应该如何:

enter image description here

现在的情况:

enter image description here

- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation {
    MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:@"driverPinId"];

    if (!annotationImage) {
        UIImage *image = [UIImage imageNamed:@"DriverPin"];
        annotationImageWithImage:image reuseIdentifier:@"driverPinId"];
    }

    return annotationImage;
}

最佳答案

您应该使用 MGLAnnotationView 而不是 MGLAnnotationImage 因为 MGLAnnotationView a descendant of UIView所以你可以像普通的 UIView 一样向它添加任何你想要的东西。

func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
    guard annotation is MGLPointAnnotation else {
        return nil
    }
    guard let castAnnotation = annotation as? MapPointMarker else {
        return nil
    }
    if (!castAnnotation.willUseImage) {
        return nil;
    }

    let identifier = castAnnotation.imageName!
    var image: UIImage = UIImage(named: castAnnotation.imageName!)!

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

    if annotationView == nil {
        annotationView = MGLAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        let imageView = UIImageView(image: image)
        annotationView!.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
        annotationView?.addSubview(imageView)
        annotationView?.centerOffset = CGVector(dx: 0, dy: -image.size.height / 2.0)
    }

    return annotationView
}

在上面的代码片段中,我将 UIImageView 添加到 annotationView 并使用 annotationView?.centerOffset = CGVector(dx: 0, dy: -image.size.height/2.0) 向上移动图像。

希望这对您有所帮助。

关于ios - Mapbox Annotation 图像居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37385924/

相关文章:

.net - 使用包含丹麦字符的 json 数据调用时出现 WCF Web 服务请求错误

ios - 创建一个显示 UIAlertView 的实用程序类

objective-c - 'k' 前缀在 Apple 的 API 中表示什么?

javascript - 如何在mapbox gl中向圆形类型添加框阴影?

javascript - mapbox.js 上的自动完成输入

objective-c - 自定义 UITableViewController 单元格 segue 不推送

ios - 垂直显示旋转图像

ios - 如何在 Xcode 5 中使用不同的图像 iOS 6 和 iOS 7

ios - Objective-C - 从核心数据中检索一对多关系数据

iOS 开发 : Using a custom image for the user annotation in MapBox