Within the code beneath, I need to enable the person to later edit the properties of a particular location in a brand new View as a sheet view modifier connected to the Map. It isn’t launching after I lengthy press a beforehand chosen location.
import SwiftUI
import MapKit
struct ContentView: View {
@State personal var userLocations = [UserLocation]()
@State personal var selectedPlace: UserLocation? = nil
let startPosition = MapCameraPosition.area(
MKCoordinateRegion(
heart: CLLocationCoordinate2D(latitude: 42.196, longitude: 24.747),
span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10)
)
)
var physique: some View {
MapReader { proxy in
Map(initialPosition: startPosition) {
ForEach(userLocations) { location in
Annotation(location.identify, coordinate: location.coordinate) {
Picture(systemName: "star.circle")
.resizable()
.foregroundStyle(.pink)
.body(width: 44, peak: 44)
.background(.white)
.clipShape(Circle())
.onLongPressGesture {
selectedPlace = location
}
}
}
}
.onTapGesture { place in
if let coordinate = proxy.convert(place, from: .native) {
let newLocation = UserLocation(
id: UUID(),
identify: "New Location",
description: "",
latitude: coordinate.latitude,
longitude: coordinate.longitude
)
userLocations.append(newLocation)
}
}
.sheet(merchandise: $selectedPlace) { place in
Textual content(place.identify)
}
}
}
}
Under is the situation information struct in a separate Swift file
import Basis
import MapKit
struct UserLocation: Codable, Equatable, Identifiable {
let id: UUID
var identify: String
var description: String
var latitude: Double
var longitude: Double
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
}
I added prints in motion locations (onTapGesture, onLongPressGesture). The print in onTapGesture executes, and the areas do show on choice.
However no print on onLongPressGesture. Which suggests the LongPressGesture does not get triggered on Lengthy Press. And that is past my understanding for the time being.
PS: SwiftUI newbie right here. Thanks upfront in your assist.