iOS Development

ios – How do I exploit Look Round View with Customized Annotations?

Spread the love


On this Swift undertaking, I’ve a map with a customized annotation which is close to the Apple infinite loop (so I can check it on a simulator). This annotation works.

When the marker for this location is tapped, a sheet that presents particulars concerning the location seems. That is the place the Look Round View that was made out there with iOS 17 must be however it’s not working. I’m uncertain of learn how to get it to work, I assume all I have to do is move via the coordinates however I should be lacking one thing.

Right here is the complete code so you may check this (excluding the information.plist half):

struct House: View {

    @Namespace non-public var locationSpace
    @State non-public var cameraPosition: MapCameraPosition = .area(.myRegion)
    @State non-public var mapSelection: MKMapItem?
    @State non-public var viewingRegion: MKCoordinateRegion?
    @State non-public var showDetails: Bool = false
    @State non-public var lookAroundScene: MKLookAroundScene?
    @State non-public var myAnnotations: [MKMapItem] = []
    let locationManager = CLLocationManager()
    
    var physique: some View {
       
        NavigationStack {
            Map(place: $cameraPosition, choice: $mapSelection, scope: locationSpace) {

                ForEach(myAnnotations, id: .self) { mapItem in
                    
                    let placemark = mapItem.placemark
                    
                    Marker(placemark.title ?? "", coordinate: placemark.coordinate)
                        .tint(.inexperienced)
                }
                
                UserAnnotation()
            }
            .onAppear {
                locationManager.requestWhenInUseAuthorization()
               
                MKMapView.look().userTrackingMode = .observe
                fetchMyAnnotations()
            }
            .mapStyle(.commonplace(pointsOfInterest: .excludingAll))
            .onMapCameraChange({ ctx in
                viewingRegion = ctx.area
            })
            .overlay(alignment: .topTrailing) {
                VStack(spacing: 15) {
                    MapCompass(scope: locationSpace)
                    
                    MapUserLocationButton(scope: locationSpace)
                }
                .buttonBorderShape(.circle)
                .padding()
            }
            .mapScope(locationSpace)
            .sheet(isPresented: $showDetails, onDismiss: {
                withAnimation(.snappy) {

                }
            }, content material: {
                MapDetails()
                    .presentationDetents([.height(800)])
                    .presentationBackgroundInteraction(.enabled(upThrough: .peak(800)))
                    .presentationCornerRadius(25)
                    .interactiveDismissDisabled(true)
            })
        }
        .onChange(of: mapSelection) { oldValue, newValue in
            
            showDetails = newValue != nil
           
            if let mapSelection = mapSelection {
                fetchLookAroundPreview()
            }
        }
    }
    
    @ViewBuilder
    func MapDetails() -> some View {
        VStack(spacing: 15) {
            HStack {
                Spacer()
                Button(motion: {
                    
                    showDetails = false
                    withAnimation(.snappy) {
                        mapSelection = nil
                    }
                }, label: {
                    Picture(systemName: "xmark.circle.fill")
                        .font(.title)
                        .foregroundStyle(.black)
                        .background(.white, in: .circle)
                })
            }
            ZStack {
                // Go searching API
                if lookAroundScene == nil {
                    /// New Empty View API
                    ContentUnavailableView("No Preview Obtainable", systemImage: "eye.slash")
                } else {
                    LookAroundPreview(scene: $lookAroundScene)
                }
            }
            .body(peak: 200)
            .clipShape(.rect(cornerRadius: 15))
            Spacer()
        }
        .padding(15)
    }
    
    func fetchMyAnnotations() {
        let coordinates = CLLocationCoordinate2D(latitude: 37.33489680799126, longitude: -122.03039070251354)
        let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinates))
        mapItem.title = "Annotation Identify"

        myAnnotations.append(mapItem)
    }

    func fetchLookAroundPreview() {
        if let mapSelection {
            
            lookAroundScene = nil
            Activity.indifferent(precedence: .background) {
                let request = MKLookAroundSceneRequest(mapItem: mapSelection)
                lookAroundScene = strive? await request.scene
            }
        }
    }
}

extension MKMapRect {
    func reducedRect(_ fraction: CGFloat = 0.35) -> MKMapRect {
        var regionRect = self

        let wPadding = regionRect.dimension.width * fraction
        let hPadding = regionRect.dimension.peak * fraction
                    
        regionRect.dimension.width += wPadding
        regionRect.dimension.peak += hPadding
                    
        regionRect.origin.x -= wPadding / 2
        regionRect.origin.y -= hPadding / 2
        
        return regionRect
    }
}

extension CLLocationCoordinate2D {
    static var myLocation: CLLocationCoordinate2D {
        return .init(latitude: 37.3346, longitude: -122.0090)
    }
}

extension MKCoordinateRegion {
    static var myRegion: MKCoordinateRegion {
        return .init(heart: .myLocation, latitudinalMeters: 10000, longitudinalMeters: 10000)
    }
}

How do I exploit the Look Round View that’s out there in iOS 17?

2 thoughts on “ios – How do I exploit Look Round View with Customized Annotations?

  1. I do believe all the ideas youve presented for your post They are really convincing and will certainly work Nonetheless the posts are too short for novices May just you please lengthen them a little from subsequent time Thanks for the post

Leave a Reply

Your email address will not be published. Required fields are marked *