当我缩放GMaps自定义markerview改变位置

时间:2017-05-05 16:01:39

标签: ios swift google-maps-markers

我正在使用Google地图,我创建了自定义标记视图并将其设置为我的标记

marker.iconView = CustomMarkerView(frame: CGRect(x: 0, y: 0, width: 198, height: 34))

这是我的自定义标记视图

import UIKit
class CustomMarkerView: UIView {
    var logo = UIImage()
    var adress = UILabel()
    var price = UILabel()
    var icon_like = UIImage(named: "icon_like")
    var icon_direction = UIImage(named: "icon_direction")

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addCustomView()
    }

    init(frame: CGRect, logo: String, adress: String, price: String) {
        super.init(frame: frame)
        self.logo = UIImage(named: logo)!
        self.adress.text = adress
        self.price.text = price
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func addCustomView() {
        self.layer.backgroundColor = UIColor(patternImage: UIImage(named: "bgMarkerView")!).cgColor
    }
}

一切正常,直到我开始缩放地图。如果我缩放它,标记视图将改变位置,但位置坐标不会改变:

Screenshot of marker position while zoomed out. Marker is to the left of the river.

Screenshot of marker position while zoomed in. Marker is now to the right of the river.

我明白这是视图表示的问题,因为实际上marker.position不会改变,但正如你在屏幕上看到的那样,它会改变

1 个答案:

答案 0 :(得分:0)

我想谷歌试图在这里做的是保持"锚点"在同一个坐标上。默认情况下,定位点是自定义UIView底部边界的中点。

我看到你要做的是显示一个infoView。如果是这种情况,您可以使用委托方法:

- (nullable UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker