From apple documentation Meet MapKit for SwiftUI video I’ve tried to comply with the video tutorial and use UserAnnotation()
perform with a purpose to get the person location in simulator as just like the video. Nevertheless it’s not working.
BeantownButtons.swift
import SwiftUI
import MapKit
@obtainable(iOS 17.0, *)
struct BeantownButtons: View {
@Binding var searchResults: [MKMapItem]
@Binding var place: MapCameraPosition
var visibleRegion: MKCoordinateRegion?
func search(for question: String) {
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = question
request.resultTypes = .pointOfInterest
request.area = visibleRegion ?? MKCoordinateRegion(
heart: .parking,
span: MKCoordinateSpan (latitudeDelta: 0.0125, longitudeDelta: 0.0125))
Process {
let search = MKLocalSearch(request: request)
let response = strive? await search.begin()
searchResults = response?.mapItems ?? []
print("Search Outcomes: (searchResults)")
}
}
var physique: some View {
HStack {
Button {
search(for: "playground")
} label: {
Label("Playgrounds", systemImage: "determine.and.youngster.holdinghands")
}
.buttonStyle(.borderedProminent)
Button {
search(for: "seaside")
} label: {
Label("Seashores", systemImage: "seaside.umbrella")
}
.buttonStyle(.borderedProminent)
Button {
place = .area(.boston)
} label: {
Label("Boston", systemImage: "constructing.2")
}
.buttonStyle(.bordered)
Button {
place = .area(.northShore)
} label: {
Label("North Shore", systemImage: "water.waves")
}
.buttonStyle(.bordered)
}
.labelStyle(.iconOnly)
}
}
#Preview {
if #obtainable(iOS 17.0, *) {
BeantownButtons(searchResults: .fixed([]), place: .fixed(.automated))
} else {
EmptyView()
}
}
ItemInfoView.swift
import SwiftUI
import _MapKit_SwiftUI
@obtainable(iOS 17.0, *)
struct ItemInfoView: View {
// ItemInfoView.swift - Fetch a Look Round scene
@State var lookAroundScene: MKLookAroundScene?
@State var route: MKRoute?
@State var selectedResult: MKMapItem
func getLookAroundScene () {
lookAroundScene = nil
Process {
let request = MKLookAroundSceneRequest(mapItem: selectedResult)
lookAroundScene = strive? await request.scene
}
}
// ItemInfoView.swift - Format journey time for show
var travelTime: String? {
guard let route else { return nil }
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .abbreviated
formatter.allowedUnits = [.hour, .minute]
return formatter.string(from: route.expectedTravelTime)
}
var physique: some View {
LookAroundPreview(initialScene: lookAroundScene)
.overlay(alignment: .bottomTrailing) {
HStack {
Textual content ("(selectedResult.title ?? "")")
if let travelTime {
Textual content(travelTime)
}
}
.font(.caption)
.foregroundStyle(.white)
.padding (10)
}
.onAppear {
getLookAroundScene()
}
.onChange(of: selectedResult) {
getLookAroundScene()
}
}
}
#Preview {
if #obtainable(iOS 17.0, *) {
ItemInfoView(selectedResult: MKMapItem(placemark: MKPlacemark(coordinate: .daraz)))
} else {
EmptyView()
}
}
CustomMapViewLookAround.swift
import SwiftUI
import _MapKit_SwiftUI
@obtainable(iOS 17.0, *)
struct CustomMapViewLookAround: View {
@State non-public var place: MapCameraPosition = .automated
@State non-public var visibleRegion: MKCoordinateRegion?
@State var route: MKRoute?
@State var searchResults: [MKMapItem] = []
@State non-public var selectedResult: MKMapItem?
func getDirections() {
print("outcomes rely: (searchResults.rely)")
route = nil
guard let selectedResult else { return }
let request = MKDirections.Request()
request.supply = MKMapItem(placemark: MKPlacemark(coordinate: .parking))
request.vacation spot = selectedResult
Process {
let instructions = MKDirections(request: request)
let response = strive? await instructions.calculate()
route = response?.routes.first
}
}
var physique: some View {
Map(place: $place, choice: $selectedResult) {
Annotation("Parking", coordinate: .parking) {
ZStack {
RoundedRectangle(cornerRadius: 5)
.fill(.background)
RoundedRectangle(cornerRadius: 5)
.stroke(.secondary, lineWidth: 5)
Picture(systemName: "automobile")
.padding(5)
}
}
.annotationTitles(.hidden)
ForEach(searchResults, id: .self) { lead to
Marker(merchandise: end result)
// .tag(1)
}
.annotationTitles(.hidden)
if let route {
MapPolyline(route)
.stroke(.blue, lineWidth: 5)
}
UserAnnotation()
}
.mapStyle(.customary)
.safeAreaInset(edge: .backside) {
HStack {
Spacer()
VStack(spacing: 0) {
if let selectedResult {
ItemInfoView(route: route, selectedResult: selectedResult)
.body(peak: 128)
.clipShape(RoundedRectangle (cornerRadius: 10))
.padding([.top, .horizontal])
}
BeantownButtons(searchResults: $searchResults, place: $place, visibleRegion: visibleRegion)
.padding(.prime)
}
Spacer()
}
.background(.thinMaterial)
}
.onChange(of: searchResults) {
place = .automated
}
.onMapCameraChange { context in
visibleRegion = context.area
}
.onChange(of: selectedResult) {
getDirections()
}
.mapControls {
MapUserLocationButton()
MapCompass()
MapScaleView()
}
.onAppear {
let locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
}
}
}
#Preview {
if #obtainable(iOS 17.0, *) {
CustomMapViewLookAround()
} else {
EmptyView()
}
}
Error:
CLLocationManager(<CLLocationManager: 0x600000010b20>) for <MKCoreLocationProvider: 0x600003000c60> did fail with error: Error Area=kCLErrorDomain Code=1 "(null)"
Every time I click on the situation button . It buffers endlessly. I’ve additionally add the situation entry privateness in data.plist. Furthermore, set the simulator location like this
Privateness - Location All the time Utilization Description
Privateness - Location All the time and When In Use Utilization Description