JiosDev
JiosDev

Reputation: 324

Call custom info window for google maps

I am trying to create my own info window for markers on my map. To do this, I created View.xib to configure it the way I need, and I call it from the class in which the map is implemented. The map is displayed, two markers, but without result when I tap on the markers. How can I correctly name this point ??

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
    }


}

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

    }

}

Upvotes: 0

Views: 69

Answers (1)

Bhavesh Odedara
Bhavesh Odedara

Reputation: 79

Fist of all you should set marker data like this way

        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"]
        }

And You will get marker data on this method

customInfoWindowView.swift

class func instanceFromNib() -> UIView {
        return UINib(nibName: "customInfoWindowView", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView
    }

and In you ViewController File

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()
}

I hope this will work for you

Upvotes: 2

Related Questions