AI Quellen
unknown
swift
a year ago
6.9 kB
9
Indexable
Prompt: import SwiftUI import MapKit class MyPlacemark: CLPlacemark {} struct MapView: View { // @State private var mapStyle: MapStyle = .standard var mapStyle: MapStyle{ return switch(mapStyleSelection){ case 0: .standard case 1: .hybrid case 2: .imagery default: .standard } } func check(_ counter: Int8) -> Int8{ if counter<2{ return counter+1 } else{ return 0 } } func newpos(_ counter: Int8) -> MapCameraPosition{ var latitude: Double var longitude: Double var name : String switch counter{ case 0: latitude = 48.7828538 longitude = 9.1666874 name = "dhbw" break case 1: latitude = 48.7749108 longitude = 9.1799351 name = "breuni" break case 2: latitude = 52.424879 longitude = 13.378663 name = "mbag berlin" break default: latitude = 0 longitude = 0 name = "0-0 Koords (error)" } // Marker(name, systemImage: "mappin.and.ellipse", coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude)) let position: MapCameraPosition = .item(MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude)))) return position } @State private var mapStyleSelection: Int = 0 @State private var currentPos: MapCameraPosition = .userLocation(fallback: .automatic) var body: some View { ZStack { Map(position: $currentPos) .mapStyle(mapStyle) .onAppear{ CLLocationManager().requestWhenInUseAuthorization() } VStack { Picker("", selection: $mapStyleSelection) { Text("Standard").tag(0) Text("Hybrid").tag(1) Text("Satellite").tag(2) } .pickerStyle(SegmentedPickerStyle()) HStack{ var counter: Int8 = 0 Button(action: {currentPos = .userLocation(fallback: .automatic)}, label: { Image(systemName: "location.magnifyingglass") .font(.system(size: 40)) }) Button(action: {currentPos = newpos(counter) counter = check(counter)}, label: { Image(systemName: "paperplane.circle") .font(.system(size: 40)) })} Spacer() } } } } #Preview { MapView() } Ich habe diesen code. wie kann ich beim ersten button, welcher zur user position springt, auf ein bestimmtes level zoomen? Antwort: import SwiftUI import MapKit struct MapView: View { @State private var mapStyleSelection: Int = 0 @State private var region: MKCoordinateRegion = MKCoordinateRegion( center: CLLocationCoordinate2D(latitude: 48.7828538, longitude: 9.1666874), span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) ) var mapStyle: MKMapType { switch mapStyleSelection { case 1: return .hybrid case 2: return .satellite default: return .standard } } func check(_ counter: Int8) -> Int8 { return counter < 2 ? counter + 1 : 0 } func newpos(_ counter: Int8) -> CLLocationCoordinate2D { switch counter { case 0: return CLLocationCoordinate2D(latitude: 48.7828538, longitude: 9.1666874) case 1: return CLLocationCoordinate2D(latitude: 48.7749108, longitude: 9.1799351) case 2: return CLLocationCoordinate2D(latitude: 52.424879, longitude: 13.378663) default: return CLLocationCoordinate2D(latitude: 0, longitude: 0) } } var body: some View { ZStack { MapViewRepresentable(region: $region, mapType: mapStyle) .onAppear { CLLocationManager().requestWhenInUseAuthorization() } VStack { Picker("", selection: $mapStyleSelection) { Text("Standard").tag(0) Text("Hybrid").tag(1) Text("Satellite").tag(2) } .pickerStyle(SegmentedPickerStyle()) HStack { Button(action: { if let userLocation = CLLocationManager().location?.coordinate { region = MKCoordinateRegion( center: userLocation, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) ) } }, label: { Image(systemName: "location.magnifyingglass") .font(.system(size: 40)) }) var counter: Int8 = 0 Button(action: { let newPosition = newpos(counter) region = MKCoordinateRegion( center: newPosition, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05) ) counter = check(counter) }, label: { Image(systemName: "paperplane.circle") .font(.system(size: 40)) }) } Spacer() } } } } struct MapViewRepresentable: UIViewRepresentable { @Binding var region: MKCoordinateRegion var mapType: MKMapType func makeUIView(context: Context) -> MKMapView { let mapView = MKMapView() mapView.delegate = context.coordinator mapView.showsUserLocation = true // This line enables the user location return mapView } func updateUIView(_ uiView: MKMapView, context: Context) { uiView.setRegion(region, animated: true) uiView.mapType = mapType } func makeCoordinator() -> Coordinator { Coordinator(self) } class Coordinator: NSObject, MKMapViewDelegate { var parent: MapViewRepresentable init(_ parent: MapViewRepresentable) { self.parent = parent } func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) { DispatchQueue.main.async { self.parent.region = mapView.region } } } } #Preview { MapView() }
Editor is loading...
Leave a Comment