Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用swift在谷歌地图中创建自定义图标视图?_Swift_Google Maps Sdk Ios - Fatal编程技术网

如何使用swift在谷歌地图中创建自定义图标视图?

如何使用swift在谷歌地图中创建自定义图标视图?,swift,google-maps-sdk-ios,Swift,Google Maps Sdk Ios,我需要这样做 google maps sdk for iOS中带有静态标签的标记如果有很多标记,我不建议使用iconView,因为它会使UI变得非常滞后,但下面是: 将UIView文件创建为“MarkerInfoView”,该文件将创建为MarkerInfoView.xib 然后在其中设计UI,为图标添加imageView,然后添加其他必要的视图以完成图标视图。还将标记作为图像视图包含在设计中。因为我不是100%确定,但我认为你不能在谷歌地图中同时使用iconView和icon 然后创建一个名

我需要这样做


google maps sdk for iOS中带有静态标签的标记如果有很多标记,我不建议使用iconView,因为它会使UI变得非常滞后,但下面是:

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

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

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

然后创建另一个swift文件,将其命名为
PlaceMarker
,在该文件中,您将创建一个符合GMSMarker的类,然后您将初始化视图,将其设置为
PlaceMarker
类中的
iconView
。让我们按照以下步骤进行操作:

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.

}
}
然后,当您在
ViewController
中拥有数据和googlemaps视图时,您可以设置如下:

let latitude = 101.432432 //arbitrary, should come from your source
let longitude = 34.432124 
let distance = 4
let placeName = "My place".
let marker = PlaceMarker(latitude: latitude, longitude: longitude, distance: distance, placeName: placeName)
marker.map = self.mapView // your google maps set your marker's map to it.

iconView
正在更改其位置,我们必须设置
groundAnchor
,以确保它将位于默认标记的准确位置。感谢@Hemang,如果您愿意,请随时编辑答案,以便将来为其他人提供更好的解决方案。