📜  如何从 json 文件中为 ios swift 加载 Google 地图样式? - 斯威夫特代码示例

📅  最后修改于: 2022-03-11 15:00:59.804000             🧑  作者: Mango

代码示例1
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)")
        }

    }