【问题标题】:How to change the custom pin image while zooming in MKMapView?如何在放大 MKMapView 时更改自定义图钉图像?
【发布时间】:2018-10-12 16:55:34
【问题描述】:

我用过swift 4。我正在使用MKMapkit,并为地图图钉添加了自定义注释图像。问题是,

假设,我有 2 张图片,例如 image1image2。最初,为所有地图图钉加载 image1。现在,如果我要放大地图视图意味着我需要将image1 替换为image2。同样,如果我要缩小意味着我需要将image2 替换为image 1。

那么,如果有人知道怎么做呢?

【问题讨论】:

标签: swift mapkit mkannotation mkannotationview


【解决方案1】:

我遇到过类似的问题,我就这样解决了

首先在您的地图委托中实现 regionDidChangeAnimated

    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {

    calculateZoomFactor()//Create global var named zoomFactor
    print("*************************")
    if zoomFactor > x { //Change the x value as you wish
        print("Zoom close, you can remove your annotations, and create with new image2")
    } else {
        print("Zoom out, remove annotations, and add new annotations with image 1")
    }
    print("*************************")
}

这是我的自定义 zoomFactor 计算器

    func calculateZoomFactor() {
    let zoomWidth = mapView.visibleMapRect.size.width
    let zoomFactor = (log2(zoomWidth)) - 8.785//You can change this constant also
    self.zoomFactor = zoomFactor
}

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多