所选地点的自定义信息窗口未显示(Google Maps / Places API)

时间:2017-02-28 17:18:06

标签: ios swift google-maps

我正在使用Google Places API,并实施了pickPlace功能,如下所示:

func pickPlace() {
    let center = CLLocationCoordinate2D(latitude: 40.708637, longitude: -74.014839)
    let northEast = CLLocationCoordinate2D(latitude: center.latitude + 0.001, longitude: center.longitude + 0.001)
    let southWest = CLLocationCoordinate2D(latitude: center.latitude - 0.001, longitude: center.longitude - 0.001)
    let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
    let config = GMSPlacePickerConfig(viewport: viewport)
    let placePicker = GMSPlacePicker(config: config)

    placePicker.pickPlace(callback: {(place, error) -> Void in
        if let error = error {
            print("Pick Place error: \(error.localizedDescription)")
            return
        }

        if let place = place {
            // Set place to class variable selectedPlace
            self.selectedPlace = place
            // Add marker & move camera to new place
            if self.selectedPlace != nil {
                let cam = GMSCameraPosition.camera(withTarget: (self.selectedPlace?.coordinate)!, zoom: 18)
                let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: cam)
                let newPlaceMarker = GMSMarker(position: (self.selectedPlace?.coordinate)!)
                newPlaceMarker.map = mapView
                self.navigationItem.title = self.selectedPlace?.name
                print(self.selectedPlace?.website)
                newPlaceMarker.title = self.selectedPlace?.name
                self.view = mapView
            }

        } else {
            self.navigationItem.title = "No place selected"
        }
    })
}

因此,这允许用户搜索“Starbucks”这样的地方并将place设置为变量var selectedPlace: GMSPlace?(在类范围内声明):地图移动到那里,在该位置添加标记,将navigationItem.title更改为selectedPlace.name,并将网站打印到控制台 - 到目前为止一切都很好。

现在我想点击标记时会弹出一个信息窗口,就像我用其他一些我硬编码的默认标记一样。这是infoWindow函数:

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

    let customInfoWindow = Bundle.main.loadNibNamed("CustomInfoWindow", owner: self, options: nil)?[0] as! CustomInfoWindow
    let placeName = marker.title!


    switch placeName {
    case "TGI Friday's":
        customInfoWindow.nameLbl.text = "TGI Friday's"
        customInfoWindow.detailLabel.text = "Thoroughly Average Chain Restaurant"
        customInfoWindow.placeImage.image = UIImage(named: "fridays")
        self.navigationItem.title = "TGI Friday's"
    case "George's":
        customInfoWindow.nameLbl.text = "George's"
        customInfoWindow.detailLabel.text = "Old School Diner"
        customInfoWindow.placeImage.image = UIImage(named: "georges")
        self.navigationItem.title = "George's"
    case "Reserve Cut":
        customInfoWindow.nameLbl.text = "Reserve Cut"
        customInfoWindow.detailLabel.text = "Kosher Steakhouse"
        customInfoWindow.placeImage.image = UIImage(named: "reserveCut")
        self.navigationItem.title = "Reserve Cut"
    case "O'Hara's":
        customInfoWindow.nameLbl.text = "O'Hara's"
        customInfoWindow.detailLabel.text = "Irish Pub"
        customInfoWindow.placeImage.image = UIImage(named: "oharas")
        self.navigationItem.title = "O'Hara's"
    case "Bill's Bar & Burger":
        customInfoWindow.nameLbl.text = "Bill's Bar & Burger"
        customInfoWindow.detailLabel.text = "Bar founded by Bill that also has burgers"
        customInfoWindow.placeImage.image = UIImage(named: "bills")
        self.navigationItem.title = "Bill's Bar & Burger"
    default:
        customInfoWindow.nameLbl.text = self.selectedPlace?.name
        customInfoWindow.detailLabel.text = self.selectedPlace?.formattedAddress
        customInfoWindow.placeImage.image = UIImage(named: "noImageFound")
        self.navigationItem.title = self.selectedPlace?.name
    }

    return customInfoWindow
}

当我点击我的硬编码默认标记时,我的自定义窗口弹出正常,但不是用户选择的位置。标题会弹出默认信息窗口,但不会弹出我的自定义窗口。有谁知道发生了什么以及如何让它发挥作用?

编辑:如果我print(selectedPlace?.website)处于不同的功能,那就是零。不确定为什么它在pickPlace函数中工作正常但不在外部 - 这就是我设置self.selectedPlace = place的原因,所以我可以在我的课堂上使用它。

无论如何,这就是我认为导致问题的原因,但我不知道为什么或如何解决它。所以,如果有人能帮我解决这个问题,那就太棒了。

0 个答案:

没有答案