Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SWIFT

How to load Google map styling from json file for ios 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)")
        }

    }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #load #Google #map #styling #json #file #ios
ADD COMMENT
Topic
Name
7+9 =