我正在尝试为地图上的标记创建自己的信息窗口。为此,我创建了 View.xib 以我需要的方式对其进行配置,并从实现映射的类调用它。显示地图,两个标记,但当我点击标记时没有结果。 我怎样才能正确命名这一点?
class MapView: UIView,CLLocationManagerDelegate,GMSMapViewDelegate {
@IBOutlet var contentView: UIView!
let mapView = GMSMapView(frame: CGRect.zero,camera: GMSCameraPosition.camera(withTarget: CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20), zoom: 12))
var marker1 = GMSMarker()
var marker2 = GMSMarker()
override init(frame: CGRect) {
super.init(frame: frame)
initView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initView()
}
private func initView() {
Bundle.main.loadNibNamed("MapView", owner: self, options: nil)
contentView = mapView
markers()
addSubview(mapView)
contentView.frame = self.bounds
mapView.delegate = self
}
func markers()
{
marker1.position = CLLocationCoordinate2D(latitude: -33.861, longitude: 151.20)
marker1.map = mapView
marker1.userData = ["marker": "1"]
marker2.position = CLLocationCoordinate2D(latitude: -33.859, longitude: 151.21)
marker2.map = mapView
marker2.userData = ["marker": "2"]
}
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
var markerData : [String:Any]?
if let data = marker.userData as? [String:Any] {
markerData = data
}
print(#function, "(markerData?["marker"] as? String ?? "")")
return true
}
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
return UINib(nibName: "customInfoWindowView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! customInfoWindowView
}
}
自定义信息窗口视图.swift
class customInfoWindowView: UIView {
@IBOutlet weak var contentView:UIView!
override init(frame: CGRect) {
super.init(frame: frame)
initView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initView()
}
private func initView() {
Bundle.main.loadNibNamed("customInfoWindowView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
}
}
你应该像这样设置标记数据
func markers()
{
marker1.position = CLLocationCoordinate2D(latitude: -33.861, longitude: 151.20)
marker1.map = mapView
marker1.userData = ["marker": "1"]
marker2.position = CLLocationCoordinate2D(latitude: -33.859, longitude: 151.21)
marker2.map = mapView
marker2.userData = ["marker": "2"]
}
您将获得有关此方法的标记数据
自定义信息窗口视图.swift
class func instanceFromNib() -> UIView {
return UINib(nibName: "customInfoWindowView", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView
}
和在您的视图控制器文件中
var infoWindow = customInfoWindowView()
func loadNiB() -> customInfoWindowView {
let infoWindow = customInfoWindowView.instanceFromNib() as! customInfoWindowView
return infoWindow
}
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
var markerData : [String:Any]?
if let data = marker.userData as? [String:Any] {
markerData = data
}
print(#function, "(markerData?["marker"] as? String ?? "")")
infoWindow.removeFromSuperview()
infoWindow = loadNiB()
infoWindow.frame = CGRect(x: 20, y: 0, width: self.viewMap.frame.width - 40, height: 250)
infoWindow.center = self.view.center
infoWindow.center.y = infoWindow.center.y + (infoWindow.frame.height / 2)
self.view.addSubview(infoWindow)
return true
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
infoWindow.removeFromSuperview()
}
我希望这对你有用