如何使用 swift 在 google 地图中制作自定义 iconView?
How to make a custom iconView in google maps with swift?
我需要做这样的事情
google 中带有静态标签的标记为 iOS
映射 sdk
如果有很多标记,我不建议使用 iconView,因为它会使 UI 变得很慢,但这里是:
创建一个UI查看文件"MarkerInfoView",它将被创建为MarkerInfoView.xib
然后在那里设计你的UI,为你的图标添加你的imageView,然后添加其他必要的视图来完成你的iconView。还包括设计中的标记作为 imageView。因为我不是 100% 确定,但我认为你不能在 google 地图中同时使用 iconView 和 icon。
然后创建一个名为 "MarkerInfoView.swift" 的 swift 文件,转到 MarkerInfoView.xib
和 select 它是 class as MarkerInfoView
.
然后创建另一个 swift 文件,我们称之为 PlaceMarker
,在该文件中您将创建一个符合 GMSMarker 的 class,然后您将初始化视图以设置它等于 PlaceMarker
class 中的 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
中拥有数据和 google 地图视图时,您可以设置如下:
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.
我需要做这样的事情
google 中带有静态标签的标记为 iOS
映射 sdk如果有很多标记,我不建议使用 iconView,因为它会使 UI 变得很慢,但这里是:
创建一个UI查看文件"MarkerInfoView",它将被创建为MarkerInfoView.xib
然后在那里设计你的UI,为你的图标添加你的imageView,然后添加其他必要的视图来完成你的iconView。还包括设计中的标记作为 imageView。因为我不是 100% 确定,但我认为你不能在 google 地图中同时使用 iconView 和 icon。
然后创建一个名为 "MarkerInfoView.swift" 的 swift 文件,转到 MarkerInfoView.xib
和 select 它是 class as MarkerInfoView
.
然后创建另一个 swift 文件,我们称之为 PlaceMarker
,在该文件中您将创建一个符合 GMSMarker 的 class,然后您将初始化视图以设置它等于 PlaceMarker
class 中的 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
中拥有数据和 google 地图视图时,您可以设置如下:
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.