【问题标题】:Custom google maps style takes a second to load自定义谷歌地图样式需要一秒钟才能加载
【发布时间】:2019-02-05 17:42:35
【问题描述】:

我正在为我在 XCode 9 w/Swift 4 中开发的公共汽车交通应用程序的 google 地图视图实现自定义样式。每当我加载地图视图时,加载时间总是不到一秒自定义样式,我不确定是什么导致了这种情况发生。

这是我所描述的效果:

如您所见,棕褐色背景是 google 地图视图的默认样式,并且仅在很短的时间内可见。

这是我实现地图视图的代码:

class StopPredictionVC: UIViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setUpMapView()
    }

    private func setUpMapView() {
        let stopLatitude = Double(stop!.lat)
        let stopLongitude = Double(stop!.lon)

        let camera = GMSCameraPosition.camera(withLatitude: stopLatitude!, longitude: stopLongitude!, zoom: 16.4)
        let frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        mapView = GMSMapView.map(withFrame: frame, camera: camera)

        let nightModeEnabled = APIWrapper.sharedInstance.settings.nightMode!

        if nightModeEnabled {
            mapView.mapStyle(withFilename: "nightStyle", andType: "json")
        } else {
            mapView.mapStyle(withFilename: "mapStyle", andType: "json")
        }

        let marker = GMSMarker()
        marker.icon = #imageLiteral(resourceName: "marker")
        marker.appearAnimation = .pop
        marker.position = CLLocationCoordinate2D(latitude: stopLatitude!, longitude: stopLongitude!)
        marker.title = "Bus Stop Name"
        marker.snippet = "Example description"
        marker.map = mapView

        buildRoute(routePath: routeConfig!.path)

        view.addSubview(mapView)
    }
}

extension GMSMapView {
    func mapStyle(withFilename name: String, andType type: String) {
        do {
            if let styleURL = Bundle.main.url(forResource: name, withExtension: type) {
                self.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
            } else {
                NSLog("Unable to find style.json")
            }
        } catch {
            NSLog("One or more of the map styles failed to load. \(error)")
        }
    }
}

buildRoute(routePath:) 是一个函数,如果有人想知道,它会在道路上构建蓝色路径。

显然这不是一个巨大的错误,但每次加载地图视图时都会令人沮丧。有人在我的代码中看到任何可能导致此问题的内容吗?

【问题讨论】:

    标签: ios swift google-maps-api-3


    【解决方案1】:

    你在 viewWillAppear 内部调用它有两个问题,首先它在 viewDidLoad 之后被调用(有一些延迟),其次它可能在每次显示 VC 时被调用,例如从推送/关闭模式返回

    所以在viewDidLoad 内调用setUpMapView

    请注意,如果 buildRoute(routePath: routeConfig!.path) 调用任何 Web 服务或在 mainQueue 中执行任何长时间工作,它应该在后台队列中

    顺便说一句,我也可以尝试加载

    GMSMapStyle(contentsOfFileURL: styleURL)
    

    在后台线程中和在 mainQueue 中设置加载后,并且只有在后台线程中允许使用GMSMapStyle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      相关资源
      最近更新 更多