我正在尝试从json文件为我的Google地图加载样式。但这给了我一个错误。我在中添加了api的密钥,在上AppDelegate.swift
添加了密钥info.plist
,但是卡仍然是标准的。
请告诉我我哪里做错了。(一种或多种地图样式无法加载。错误)
GoogleMapCustomView.swift
import UIKit
import GoogleMaps
class MapView: UIView {
@IBOutlet var contentView: UIView!
//MARK: OVerride
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
//MARK: Property
private func commonInit() {
Bundle.main.loadNibNamed("MapView", owner: self, options: nil)
setupMap()
addSubview(contentView)
contentView.frame = self.bounds
}
func setupMap() {
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.settings.myLocationButton = true
mapView.delegate = self as! GMSMapViewDelegate
contentView = mapView
//style map
do{
if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json")
{
mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
}else{
print("unable to find style.json")
}
}catch{
print("One or more of the map styles failed to load.\(error)")
}
}
style.json
[
{
"featureType": "road",
"elementType": "fill",
"stylers": [
{ "color": "#99FF33" }
]
]
您的json无效尝试
[
{
"featureType": "road",
"elementType": "fill",
"stylers": [
{ "color": "#99FF33" }
]
} <<<<<< you miss this
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句