Created
September 12, 2021 18:55
-
-
Save nitrag/0f36adb8e85e5b8df992a22226c844a2 to your computer and use it in GitHub Desktop.
Basic (iOS / Swift) CarPlay Map App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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