在iOS中添加JSON文件格式谷歌地图



我正在尝试从json文件中为我的谷歌地图加载样式。 但它给了我一个错误。我在AppDelegate.swift中添加了api的密钥,我添加了info.plist的密钥,但卡仍然是标准的。

请告诉我我在哪里犯了错误。(一个或多个地图样式加载失败。错误(

谷歌地图自定义视图.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 
]

最新更新