Skip to content

Instantly share code, notes, and snippets.

@nitrag
Created September 12, 2021 18:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitrag/0f36adb8e85e5b8df992a22226c844a2 to your computer and use it in GitHub Desktop.
Save nitrag/0f36adb8e85e5b8df992a22226c844a2 to your computer and use it in GitHub Desktop.
Basic (iOS / Swift) CarPlay Map App
class CarPlayMapView: UIViewController, CPMapTemplateDelegate {
var mapView: MKMapView?
override func viewDidLoad() {
super.viewDidLoad()
print("loading the carplay mapview")
let region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 0, longitude: 0),
span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)
)
self.mapView = MKMapView(frame: view.bounds)
self.mapView!.setRegion(region, animated: true)
self.mapView!.showsUserLocation = true
self.mapView!.setUserTrackingMode(.follow, animated: true)
self.mapView!.overrideUserInterfaceStyle = .light
self.mapView!.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.mapView!)
self.mapView!.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
self.mapView!.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
self.mapView!.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
self.mapView!.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
}
func mapTemplateDidBeginPanGesture(_ mapTemplate: CPMapTemplate) {
print("πŸš™πŸš™πŸš™πŸš™πŸš™ Panning")
}
func mapTemplate(_ mapTemplate: CPMapTemplate, panWith direction: CPMapTemplate.PanDirection) {
print("πŸš™πŸš™πŸš™πŸš™πŸš™ Panning: \(direction)")
}
}
import UIKit
import SwiftUI
import CarPlay
@available(iOS 14.0, *)
class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate {
var carWindow: CPWindow?
var interfaceController: CPInterfaceController?
//var locationService: LocationService
var mapTemplate: CPMapTemplate?
var mapView: CarPlayMapView?
// CarPlay connected
func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didConnect interfaceController: CPInterfaceController, to window: CPWindow) {
print("πŸš™πŸš™πŸš™πŸš™πŸš™ Connected to CarPlay.")
self.interfaceController = interfaceController
initTemplates()
self.mapView = CarPlayMapView()
window.rootViewController = self.mapView
self.carWindow = window
self.carWindow?.isUserInteractionEnabled = true
self.carWindow?.isMultipleTouchEnabled = true
self.interfaceController?.setRootTemplate(self.mapTemplate!, animated: true, completion: {_, _ in })
}
// CarPlay disconnected
private func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didDisconnect interfaceController: CPInterfaceController) {
print("πŸš™πŸš™πŸš™πŸš™πŸš™ Disconnected from CarPlay.")
self.interfaceController = nil
}
private func getMapBarButtons() -> [CPBarButton] {
return [
CPBarButton(image: UIImage(systemName: "location")!, handler: { _ in
print("πŸš™πŸš™πŸš™πŸš™πŸš™ Tapped locate me")
})
]
}
private func initTemplates() {
self.mapTemplate = CPMapTemplate()
self.mapTemplate?.automaticallyHidesNavigationBar = false
self.mapTemplate?.trailingNavigationBarButtons.append(contentsOf: getMapBarButtons())
self.mapTemplate?.showPanningInterface(animated: true)
self.mapTemplate?.mapDelegate = self.mapView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment