Skip to main content

Map styles

Set the map to night, day, or satellite.

Map Styles

import Foundation
import UIKit
import TrimbleMaps
import TrimbleMapsAccounts

class MapStylesViewController: UIViewController, AccountManagerDelegate {

    internal var mapView: TMGLMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let apiKey =  "Your-API-key-here"
        let account = Account(apiKey: apiKey, region: Region.northAmerica)
        AccountManager.default.account = account
        AccountManager.default.delegate = self
    }

    func stateChanged(newStatus: AccountManagerState) {
        if newStatus == .loaded {
            DispatchQueue.main.async {
                // Create a map view
                self.mapView = TMGLMapView(frame: self.view.bounds)

                // Set the map location
                let center = CLLocationCoordinate2D(latitude: 40.7584766, longitude: -73.9840227)
                self.mapView.setCenter(center, zoomLevel: 13, animated: false)

                // Add the map
                self.view.addSubview(self.mapView)

                // Add the style buttons
                self.addStyleButtons()
            }
        }
    }

    private func addButton(title: String, action: Selector) -> UIButton {
        let button = UIButton()
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 12)
        button.contentEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

        button.backgroundColor = .gray
        button.layer.borderColor = UIColor.black.cgColor
        button.layer.borderWidth = 0.5
        button.layer.cornerRadius = 10.0

        button.addTarget(self, action: action, for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false

        self.mapView.addSubview(button)

        return button
    }

    func addStyleButtons() {
        let dayButton = addButton(title: "DAY", action: #selector(dayButtonPressed))
        let nightButton = addButton(title: "NIGHT", action: #selector(nightButtonPressed))
        let satelliteButton = addButton(title: "SATELLITE", action: #selector(satelliteButtonPressed))

        NSLayoutConstraint.activate([
            satelliteButton.bottomAnchor.constraint(equalTo: mapView.bottomAnchor, constant: -10),
            satelliteButton.trailingAnchor.constraint(equalTo: mapView.trailingAnchor, constant: -10),
            nightButton.bottomAnchor.constraint(equalTo: satelliteButton.topAnchor, constant: -5),
            nightButton.trailingAnchor.constraint(equalTo: mapView.trailingAnchor, constant: -10),
            nightButton.leadingAnchor.constraint(equalTo: satelliteButton.leadingAnchor, constant: 0),
            dayButton.bottomAnchor.constraint(equalTo: nightButton.topAnchor, constant: -5),
            dayButton.trailingAnchor.constraint(equalTo: mapView.trailingAnchor, constant: -10),
            dayButton.leadingAnchor.constraint(equalTo: satelliteButton.leadingAnchor, constant: 0),
        ])

    }

    @objc func dayButtonPressed() {
        mapView.styleURL = TMGLStyle.mobileDayStyleURL
    }

    @objc func nightButtonPressed() {
        mapView.styleURL = TMGLStyle.mobileNightStyleURL
    }

    @objc func satelliteButtonPressed() {
        mapView.styleURL = TMGLStyle.satelliteStyleURL
    }

}
Last updated February 9, 2024.