Google Sdk中的didTapMarker无法正常工作

时间:2018-02-25 08:17:25

标签: swift google-maps

嗨我在ViewdidLoad中添加了地图中的纬度和经度标记,我希望当用户点击标记时,在第一个我的功能中,当我在用户点击标记打印“taped”时,我想要的视图中的xib! 我的班级:

class MapViewController: UIViewController {
    var ShopsInMap:[ObjectShop] = []
  var gmsMap = GMSMapView()
    override func viewDidLoad() {
        super.viewDidLoad()
        gmsMap.delegate = self
        setMarker()
    }
override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    self.gmsMap.frame = self.view.bounds
}

我使用此功能添加标记:

func setMarker(){
        let camera = GMSCameraPosition.camera(withLatitude:35.6892 , longitude: 51.3890, zoom: 10.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

        view = mapView
        for shopvalue in ShopsInMap{
            if (shopvalue.lat != ""){
                let marker = GMSMarker()
                print("lat : \(Double(shopvalue.lat)!) lng is : \(Double(shopvalue.lng)!)")
                marker.position = CLLocationCoordinate2D(latitude: Double(shopvalue.lat)!, longitude: Double(shopvalue.lng)!)
                marker.title = "\(shopvalue.address) \n \(shopvalue.name)"
                marker.snippet = shopvalue.name
                marker.map = mapView
            }
        }
    }

我的分机是:

extension MapViewController :GMSMapViewDelegate{
    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        print("marker tap : \(marker.title)")
        return false
    }
    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
        print("taped is : \(coordinate.latitude)")
    }
    func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {

    }
    func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
        let coustome = Bundle.main.loadNibNamed("CustomeWindowsInfo", owner: self, options: nil) as! CustomeWindowsInfo
        coustome.test.text = marker.title
        print("taped ifno : \(marker.title)")
    }
}

我在堆栈溢出中读到了所有问题,但我可以使用这个函数!! 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

您需要在viewController.view中添加地图并调整框架

class MapViewController: UIViewController {
    var ShopsInMap:[ObjectShop] = []
    var gmsMap = GMSMapView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubView(gmsMap)
        gmsMap.delegate = self
        setMarker()
    }

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.gmsMap.frame = self.view.bounds
}


func setMarker(){
        for shopvalue in ShopsInMap{
            if (shopvalue.lat != ""){
                let marker = GMSMarker()
                print("lat : \(Double(shopvalue.lat)!) lng is : \(Double(shopvalue.lng)!)")
                marker.position = CLLocationCoordinate2D(latitude: Double(shopvalue.lat)!, longitude: Double(shopvalue.lng)!)
                marker.title = "\(shopvalue.address) \n \(shopvalue.name)"
                marker.snippet = shopvalue.name
                marker.map = self.gmsMap
            }
        }
    }