I have an existing iOS app with MapKit. It always shows the current user location with UserAnnotation. But the same isn't true for macOS. I have this sample macOS application in SwiftUI. In the following, the current user location with a large blue dot appears only occasionally. It won't, 19 of 20 times. Why is that? I do have a location privacy key in Info.plist. And the Location checkbox is on under Signing & Capabilities.
import SwiftUI
import MapKit
struct ContentView: View {
@State private var markerItems: [MarkerItem] = [
MarkerItem(name: "Farmers Market 1", lat: 35.681, lon: 139.691),
MarkerItem(name: "Farmers Market 2", lat: 35.685, lon: 139.695),
MarkerItem(name: "Farmers Market 3", lat: 35.689, lon: 139.699)
]
@State private var position: MapCameraPosition = .automatic
var body: some View {
Map(position: $position) {
UserAnnotation()
ForEach(markerItems, id: \.self) { item in
Marker(item.name, coordinate: CLLocationCoordinate2D(latitude: item.lat, longitude: item.lon))
}
}
.mapControlVisibility(.hidden)
.mapStyle(.standard(elevation: .realistic))
.ignoresSafeArea()
}
}
#Preview {
ContentView()
}
struct MarkerItem: Hashable {
let name: String
let lat: Double
let lon: Double
}