Google 地图 iOS 的位置标记信息窗口

Position marker Infowindows for Google Maps iOS

我正在实施以在 google 地图中显示多个标记,直到现在它正在运行,我免除了 Infowindows。

在我的项目中我有: - AppDelegate.swift - ViewController.swift - CustomInfoWindow.xib(两个标签和一个按钮) - CustomInfoWindow.swift(标签和底部为 IB)

详情:

ViewController.swift

import UIKit
import GoogleMaps

class ViewController: UIViewController, GMSMapViewDelegate {


    // Properties
    var map:GMSMapView!
    var longitudes:[Double]!
    var latitudes:[Double]!
    var architectNames:[String]!
    var completedYear:[String]!

    override func viewDidLoad() {
        super.viewDidLoad()


        //
        latitudes = [48.8566667,41.8954656,51.5001524]
        longitudes = [2.3509871,12.4823243,-0.1262362]
        architectNames = ["Stephen Sauvestre","Bonanno Pisano","Augustus Pugin"]
        completedYear = ["1889","1372","1859"]
        //
        self.map = GMSMapView(frame: self.view.frame)
        self.view.addSubview(self.map)
        self.map.delegate = self

        // Add 3 markers
        for i in 0...2 {
            let coordinates = CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i])
            let marker = GMSMarker(position: coordinates)
            marker.map = self.map
            marker.icon = UIImage(named: "pin")
            marker.infoWindowAnchor = CGPoint(x: 0.5, y: 0.2)
            marker.accessibilityLabel = "\(i)"
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    class func instanceFromNib() -> CustomInfoWindow {
        return UINib(nibName: "CustomInfoWindow", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomInfoWindow
    }

    var tappedMarker = GMSMarker()
    //var infoWindow = CustomInfoWindow()


    var infoWindow = CustomInfoWindow()


    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {

        let index:Int! = Int(marker.accessibilityLabel!)
        infoWindow = ViewController.instanceFromNib()
        infoWindow.architectLbl.text = architectNames[index]
        infoWindow.completedYearLbl.text = completedYear[index]
        infoWindow.infoBtn.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        self.view.addSubview(infoWindow)
        return false
    }

    func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
        return UIView()
    }


    @objc func buttonTapped(sender: UIButton) {
        print("Yeah! Button is tapped!")     
    }


 }

CustomInfoWindow.swift

import UIKit

class CustomInfoWindow: UIView {

    @IBOutlet var completedYearLbl: UILabel!
    @IBOutlet var architectLbl: UILabel!
    @IBOutlet weak var infoBtn: UIButton!

}

我遇到的问题是,当我点击市场时,它似乎固定在屏幕上,而我希望它出现在市场顶部,如果我移动地图,它也会移动。

See image

关于改进该细节代码的任何建议。

您尚未定义:mapView.projection.point,函数:didChange positiondidTapAt

这是更新后的代码:

import UIKit
import GoogleMaps

class ViewController: UIViewController, GMSMapViewDelegate {

    // Properties
    var map:GMSMapView!
    var longitudes:[Double]!
    var latitudes:[Double]!
    var architectNames:[String]!
    var completedYear:[String]!


    override func viewDidLoad() {
        super.viewDidLoad()


        //
        latitudes = [48.8566667,41.8954656,51.5001524]
        longitudes = [2.3509871,12.4823243,-0.1262362]
        architectNames = ["Stephen Sauvestre","Bonanno Pisano","Augustus Pugin"]
        completedYear = ["1889","1372","1859"]
        //
        self.map = GMSMapView(frame: self.view.frame)
        self.view.addSubview(self.map)
        self.map.delegate = self

        // Add 3 markers
        for i in 0...2 {
            let coordinates = CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i])
            let marker = GMSMarker(position: coordinates)
            marker.map = self.map

            marker.icon = UIImage(named: "new-pin")

            marker.accessibilityLabel = "\(i)"
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    class func instanceFromNib() -> CustomInfoWindow {
        return UINib(nibName: "CustomInfoWindow", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomInfoWindow
    }

    var tappedMarker = GMSMarker()
    var infoWindow = CustomInfoWindow()


    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {

        let index:Int! = Int(marker.accessibilityLabel!)
        print(index)

        let location = CLLocationCoordinate2D(latitude: marker.position.latitude, longitude: marker.position.longitude)

        tappedMarker = marker
        infoWindow.removeFromSuperview()

        infoWindow = ViewController.instanceFromNib()



        infoWindow.architectLbl.text = architectNames[index]
        infoWindow.completedYearLbl.text = completedYear[index]

        infoWindow.center = mapView.projection.point(for: location)

        infoWindow.center.y = infoWindow.center.y - 107
        infoWindow.infoBtn.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)

        self.view.addSubview(infoWindow)

        return false
    }

    func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
        return UIView()
    }



    func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {

        let location = CLLocationCoordinate2D(latitude: tappedMarker.position.latitude, longitude: tappedMarker.position.longitude)
        infoWindow.center = mapView.projection.point(for: location)
        infoWindow.center.y = infoWindow.center.y - 107


    }


    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
        infoWindow.removeFromSuperview()

    }



    @objc func buttonTapped(sender: UIButton) {
        print("Yeah! Button is tapped!")

    }


 }

编码愉快!