【发布时间】: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