Skip to content

Instantly share code, notes, and snippets.

@voncannon
Created May 10, 2024 16:21
Show Gist options
  • Save voncannon/3a2f10f5d4241b7dd19ab7ab77ee9583 to your computer and use it in GitHub Desktop.
Save voncannon/3a2f10f5d4241b7dd19ab7ab77ee9583 to your computer and use it in GitHub Desktop.
MapLibre Swift UI View Representation
//
// MainMapLibreViewRepresentation.swift
// Topo Lock Maps
//
// Created by VonCannon Technology, Inc. on 6/20/23.
//
import SwiftUI
import Foundation
import Mapbox
struct MainMapLibreViewRepresentation: UIViewRepresentable {
typealias UIViewType = MGLMapView
@EnvironmentObject var mapStore: MapStore
@Environment(\.managedObjectContext) private var viewContext
func makeUIView(context: Context) -> MGLMapView {
#if DEBUG
MGLLoggingConfiguration.shared.loggingLevel = .verbose
#else
MGLLoggingConfiguration.shared.loggingLevel = .warning
#endif
MGLNetworkConfiguration.sharedManager.delegate = NetworkManager.shared
let styleUrl = BaseMapMO.lastUsedBaseMap(context: viewContext)?.styleUrl ?? mapStore.selectedBaseMap?.styleUrl ?? BaseMaps.defaultBaseMap.styleUrl
let mapView = MGLMapView(frame: .zero)
mapView.delegate = context.coordinator
mapView.styleURL = styleUrl
mapView.showsScale = true
let lastMapConfig = MapStore.getLastViewedLocationAndZoom()
mapView.setCenter(CLLocationCoordinate2D(latitude: lastMapConfig.latitude, longitude: lastMapConfig.longitude), zoomLevel: lastMapConfig.zoom, animated: false)
mapView.attributionButtonPosition = .bottomLeft
mapView.logoView.isHidden = true //We aren't using mapbox tiles so hide this
mapView.compassViewMargins = CGPoint(x: 15, y: 10) //To align the compass with the map buttons
mapView.scaleBarShouldShowDarkStyles = context.environment.colorScheme == .dark
mapView.anchorRotateOrZoomGesturesToCenterCoordinate = mapStore.lockMapPanningOnZoom
switch mapStore.locationStatus {
case .NoAccess:
break
case .Access:
mapView.showsUserLocation = true
mapView.showsUserHeadingIndicator = true
case .Unknown:
mapView.showsUserLocation = false
mapView.showsUserHeadingIndicator = false
}
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleLongPress(_:)))
longPressGestureRecognizer.delegate = context.coordinator
mapView.addGestureRecognizer(longPressGestureRecognizer)
let twoFingerGestureRecognizer = UILongPressGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleTwoFinderLongPress(_:)))
twoFingerGestureRecognizer.numberOfTouchesRequired = 2
mapView.addGestureRecognizer(twoFingerGestureRecognizer)
return mapView
}
func updateUIView(_ mapView: MGLMapView, context: Context) {
mapView.scaleBarShouldShowDarkStyles = context.environment.colorScheme == .dark
}
func makeCoordinator() -> MapLibreCoordinator {
return MapLibreCoordinator(mapStore: mapStore)
}
static func dismantleUIView(_ uiView: MGLMapView, coordinator: MapLibreCoordinator) {
//Always ensure the coordinate is deallocated!
TLLog.log(type: .App, statement: "dismantleUIView - MGLMapView - Coordinator")
}
}
#if DEBUG
#Preview {
MainMapLibreViewRepresentation()
.environmentObject(MapStore.shared)
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment