【问题标题】:Position marker Infowindows for Google Maps iOS用于 Google 地图 iOS 的位置标记信息窗口
【发布时间】:2018-10-15 14:19:31
【问题描述】:

我正在实施以在谷歌地图中显示几个标记,直到现在它正在工作,我免除了 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

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

【问题讨论】:

    标签: ios swift google-maps google-maps-markers


    【解决方案1】:

    你还没有定义: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!")
    
        }
    
    
     }
    

    编码愉快!

    【讨论】:

    • 我试过了,效果很好,非常感谢您的帮助。
    猜你喜欢
    • 2016-01-20
    • 2011-02-24
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 2015-09-07
    • 2016-07-28
    相关资源
    最近更新 更多