Open event in maps

This commit is contained in:
Selim Mustafaev 2024-12-19 23:23:17 +03:00
parent 49f25e0c7e
commit 231c990fbb
4 changed files with 50 additions and 2 deletions

View File

@ -8,11 +8,31 @@
import CoreLocation import CoreLocation
struct EventModel: Identifiable { struct EventModel: Identifiable, Hashable {
let id: String let id: String
var date: String var date: String
var coordinate: CLLocationCoordinate2D var coordinate: CLLocationCoordinate2D
var address: String var address: String
var isMe: Bool var isMe: Bool
func hash(into hasher: inout Hasher) {
hasher.combine(id)
hasher.combine(date)
hasher.combine(coordinate.latitude)
hasher.combine(coordinate.longitude)
hasher.combine(address)
hasher.combine(isMe)
}
static func == (lhs: EventModel, rhs: EventModel) -> Bool {
lhs.id == rhs.id
&& lhs.date == rhs.date
&& lhs.coordinate.latitude == rhs.coordinate.latitude
&& lhs.coordinate.longitude == rhs.coordinate.longitude
&& lhs.address == rhs.address
&& lhs.isMe == rhs.isMe
}
} }

View File

@ -13,6 +13,7 @@ import MapKit
struct EventsScreen: View { struct EventsScreen: View {
@State var viewModel: EventsViewModel @State var viewModel: EventsViewModel
@State var selectedEvent: EventModel?
init(viewModel: EventsViewModel) { init(viewModel: EventsViewModel) {
self.viewModel = viewModel self.viewModel = viewModel
@ -61,9 +62,10 @@ struct EventsScreen: View {
} }
var map: some View { var map: some View {
Map { Map(selection: $selectedEvent) {
ForEach(viewModel.events) { ForEach(viewModel.events) {
Marker($0.date, coordinate: $0.coordinate) Marker($0.date, coordinate: $0.coordinate)
.tag($0)
} }
} }
} }
@ -115,6 +117,14 @@ struct EventsScreen: View {
} label: { } label: {
Label(useLabels ? "Copy" : "", systemImage: "doc.on.doc") Label(useLabels ? "Copy" : "", systemImage: "doc.on.doc")
} }
if useLabels {
Button() {
viewModel.openMaps(event)
} label: {
Label("Open in Maps", systemImage: "map")
}
}
} }
} }

View File

@ -11,6 +11,7 @@ import AutoCatCore
import CoreLocation import CoreLocation
import UniformTypeIdentifiers import UniformTypeIdentifiers
import MobileCoreServices import MobileCoreServices
import MapKit
extension UTType { extension UTType {
@ -174,4 +175,19 @@ class EventsViewModel: ACHudContainer {
return date + "\n" + location return date + "\n" + location
} }
func openMaps(_ event: EventModel) {
guard let url = URL(string: "yandexmaps://maps.yandex.ru/?pt=\(event.coordinate.longitude),\(event.coordinate.latitude)&z=12") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
} else {
let placemark = MKPlacemark(coordinate: event.coordinate)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = event.date
mapItem.openInMaps()
}
}
} }

View File

@ -415,3 +415,5 @@
"Server" = "Сервер"; "Server" = "Сервер";
"Not updated" = "Не обновленные"; "Not updated" = "Не обновленные";
"Open in Maps" = "Открыть на карте";