【问题标题】:add json file style google map in ios在 ios 中添加 json 文件样式的谷歌地图
【发布时间】:2020-01-11 17:05:37
【问题描述】:

我正在尝试从 json 文件加载我的谷歌地图的样式。 但是它给了我一个错误。我在AppDelegate.swift中添加了api的密钥,我在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" }
        ]
]

【问题讨论】:

  • this - 一个或多个地图样式加载失败。错误

标签: ios json swift google-maps


【解决方案1】:

你的json无效试试

[
    {
        "featureType": "road",
         "elementType": "fill",
        "stylers": [
            { "color": "#99FF33" }
        ]
    } <<<<<< you miss this 
]

【讨论】:

  • 哦,我的粗心...谢谢!
猜你喜欢
  • 2017-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多