Created
March 24, 2022 18:49
-
-
Save userow/30a166f27e39b4342d8327cf0b329a25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TestMapAppApp.swift | |
// TestMapApp | |
// | |
// Created by Pavlo Vasylenko on 21.03.2022. | |
// | |
import SwiftUI | |
import Foundation | |
import MapKit | |
@main | |
struct TestMapAppApp: App { | |
var mapModel = MapModel() | |
var body: some Scene { | |
WindowGroup { | |
ContentView().environmentObject(mapModel) | |
} | |
} | |
} | |
class MapModel: ObservableObject { | |
@Published var mapType = MKMapType.standard | |
@Published var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 35.685, longitude: 139.7514), span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)) | |
} | |
struct ContentView: View { | |
@EnvironmentObject var mapModel: MapModel | |
@State private var mapType: Int = 0 | |
@State private var mapTypes = ["Standard", | |
// "Satellite", | |
"Hybrid"] | |
var body: some View { | |
ZStack (alignment: .topLeading) { | |
MapView().edgesIgnoringSafeArea(.all) | |
mapTools | |
} | |
} | |
var mapTools: some View { | |
HStack { | |
Spacer() | |
Picker(selection: Binding<Int> ( | |
get: {self.mapType}, | |
set: { | |
self.mapType = $0 | |
self.mapModel.mapType = self.getMapType() | |
} | |
), label: Text("")) { | |
ForEach(0 ..< mapTypes.count) { | |
Text(self.mapTypes[$0]) | |
} | |
}.pickerStyle(SegmentedPickerStyle()) | |
.labelsHidden() | |
.frame(width: 222, height: 60) | |
.clipped() | |
Spacer() | |
} | |
} | |
func getMapType() -> MKMapType { | |
switch mapType { | |
case 0: return .standard | |
// case 1: return .satellite | |
case 1: return .hybrid | |
default: | |
return .standard | |
} | |
} | |
} | |
struct MapView: UIViewRepresentable { | |
@EnvironmentObject var mapModel: MapModel | |
let mapView = MKMapView() | |
func makeUIView(context: Context) -> MKMapView { | |
mapView.mapType = mapModel.mapType | |
mapView.setRegion(mapModel.region, animated: true) | |
return mapView | |
} | |
func updateUIView(_ uiView: MKMapView, context: Context) { | |
uiView.mapType = mapModel.mapType | |
} | |
} | |
//struct ContentView_Previews: PreviewProvider { | |
// @EnvironmentObject var mapModel: MapModel | |
// static var previews: some View { | |
// ContentView().environmentObject(mapModel) | |
// } | |
//} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment