如何在Google地图中快速创建自定义iconView?

时间:2018-12-18 22:10:00

标签: swift google-maps-sdk-ios

我需要做这样的事情 map with marker and label

适用于iOS的google maps sdk中带有静态标签的标记

1 个答案:

答案 0 :(得分:2)

如果会有很多标记,我不建议使用iconView,因为它会使UI变得很落后,但是在这里:

将UIView文件创建为“ MarkerInfoView”,该文件将创建为MarkerInfoView.xib

然后在其中设计UI,为图标添加imageView,然后添加其他必要的视图以完成iconView。在设计中还包括标记作为imageView。因为我不是100%肯定,但我认为您不能在Google地图中同时使用iconView和icon。

然后创建一个名为“ MarkerInfoView.swift”的快捷文件,转到MarkerInfoView.xib并将其类选择为MarkerInfoView

然后创建另一个快速文件,将其命名为PlaceMarker,在该文件内,您将创建一个符合GMSMarker的类,然后初始化视图以将其设置为等于{ {1}}类。让我们这样做如下:

iconView

然后,当您在PlaceMarker中拥有数据和googlemaps视图时,可以设置为:

class PlaceMarker: GMSMarker {
//Initialize with lat and long, then set position equal to the coordinate.
// 'position' comes from inheriting from GMSMarker, which is google marker.
init(latitude: Double, longitude: Double, distance: Double, placeName: String) {
    super.init()
    if let lat: CLLocationDegrees = latitude,
        let long: CLLocationDegrees = longitude {
        let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
        position = coordinate
    }

    let view = Bundle.main.loadNibNamed("MarkerInfoView", owner: nil, options: nil)?.first as! MarkerInfoView
    // you can set your view's properties here with data you are sending in initializer.
    // Remember if you need to pass more than just latitude and longitude, you need
    // to update initializer.
    // lets say you created 2 outlet as placeNameLabel, and distanceLabel, you can set
    // them like following:
    view.placeNameLabel.text = placeName
    view.distanceLabel.text = distance

    // Once your view is ready set iconView property coming from inheriting to
    // your view as following:

    iconView = view
    appearAnimation = .pop //not necessarily but looks nice.

}
}