首页 > 解决方案 > 使用swift 4.1在谷歌地图上自定义窗口信息

问题描述

我正在为 ios swift 4.1 中的谷歌地图上的自定义窗口信息编写此方法,但该窗口不显示在谷歌地图上

我也是代表,但没有结果

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

    let infoWindow: InfoWindow = Bundle.main.loadNibNamed("InfoWindow", owner: self.view, options: nil)!.first! as! InfoWindow

    infoWindow.numberCarLabel.text = "TLA-618"
    infoWindow.dateLabel.text = "2018-05-02 10:39:43"
    infoWindow.digreeLabel.text = "94"
    infoWindow.addressLabel.text = "Sunrise Appartments, Marine Promenade, Karachi"
    infoWindow.kilometterLabel.text = "0"

    return infoWindow

}

标签: iosswiftgoogle-mapsswift4.1

解决方案


好的,在查看您的代码后,首先您的委托函数名称中有一个错字

正确的是:

func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
     let infoWindow: InfoWindow = Bundle.main.loadNibNamed("InfoWindow", owner: self.view, options: nil)!.first! as! InfoWindow

     infoWindow.numberCarLabel.text = "TLA-618"
     infoWindow.dateLabel.text = "2018-05-02 10:39:43"
     infoWindow.digreeLabel.text = "94"
     infoWindow.addressLabel.text = "Sunrise Appartments, Marine Promenade, Karachi"
     infoWindow.kilometterLabel.text = "0"

     return infoWindow
}

您在这里也有一个问题,func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])您不需要每次CLLocationManager报告位置时都重新分配您的地图视图

用这个替换你的func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])实现

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation = locations.last

    let camera = GMSCameraPosition.camera(withLatitude: userLocation!.coordinate.latitude,
                                          longitude: userLocation!.coordinate.longitude, zoom: 13.0)
    mapView.isMyLocationEnabled = true
    self.mapView.animate(to: camera)
    locationManager.stopUpdatingLocation()
}

推荐阅读