import SwiftUI
import MapKit
import CoreLocation
struct ContentView: View {
@StateObject private var mapAPI = MapAPI()
@State private var text = ""
@State private var locationInfo: String = ""
@State private var showLocationInfo = false
@State private var imageUrls = [String]() // Array to store image URLs
@State private var locationManager = CLLocationManager()
@State private var isMovingOnMap = false
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
if manager.authorizationStatus == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
}
}
@State private var showImagePicker = false
@State private var showCamera = false
@State private var selectedImage: UIImage?
@State private var images: [UIImage] = []
@State private var selectedImageSourceType: UIImagePickerController.SourceType?
struct ImagePicker: UIViewControllerRepresentable {
typealias UIViewControllerType = UIImagePickerController
var sourceType: UIImagePickerController.SourceType
var completionHandler: (UIImage?) -> Void
func makeUIViewController(context: Context) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = sourceType
imagePicker.delegate = context.coordinator
return imagePicker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
// No update needed
}
func makeCoordinator() -> Coordinator {
Coordinator(completionHandler: completionHandler)
}
final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
private let completionHandler: (UIImage?) -> Void
init(completionHandler: @escaping (UIImage?) -> Void) {
self.completionHandler = completionHandler
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
completionHandler(nil)
picker.dismiss(animated: true)
return
}
completionHandler(image)
picker.dismiss(animated: true)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
completionHandler(nil)
picker.dismiss(animated: true)
}
}
}
var body: some View {
TabView {
// Page 1
ZStack(alignment: .topLeading) {
Map(coordinateRegion: $mapAPI.region, interactionModes: .all, showsUserLocation: true)
.edgesIgnoringSafeArea(.all)
.gesture(
DragGesture()
.onChanged { gesture in
isMovingOnMap = true
showLocationInfo = false
let translation = gesture.translation
let currentRegion = mapAPI.region
// Check if the horizontal drag distance is larger than the vertical distance
if abs(translation.width) > abs(translation.height) {
// Update the map's center coordinate based on the horizontal drag
let mapWidth = UIScreen.main.bounds.size.width
let coordinateSpan = currentRegion.span.longitudeDelta * Double(mapWidth) / 360.0
let coordinateDelta = CLLocationDegrees(translation.width) * coordinateSpan / Double(mapWidth)
let updatedCenter = CLLocationCoordinate2D(latitude: currentRegion.center.latitude, longitude: currentRegion.center.longitude - coordinateDelta)
let updatedRegion = MKCoordinateRegion(center: updatedCenter, span: currentRegion.span)
DispatchQueue.main.async {
mapAPI.region = updatedRegion
}
} else {
// Update the map's region for vertical or diagonal drags (zooming)
let span = currentRegion.span
let spanMultiplier = min(span.latitudeDelta, span.longitudeDelta) / 500.0
let latitudeDelta = span.latitudeDelta - (translation.height * spanMultiplier)
let longitudeDelta = span.longitudeDelta - (translation.width * spanMultiplier)
let updatedRegion = MKCoordinateRegion(center: currentRegion.center, span: MKCoordinateSpan(latitudeDelta: latitudeDelta, longitudeDelta: longitudeDelta))
DispatchQueue.main.async {
mapAPI.region = updatedRegion
}
}
}
.onEnded { _ in
isMovingOnMap = false
}
)
VStack {
HStack {
I am using mapkit and when i try to move around on the map i get glitches and issues can anyone fix.
Please explain clear what are the "glitches" you get.
Why do you dispatch ?
DispatchQueue.main.async {
mapAPI.region = updatedRegion
}
What happens if you call directly
mapAPI.region = updatedRegion