简单使用:显示指定位置坐标与缩放
import SwiftUI
import MapKit
struct ContentView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 30.2906511800, longitude: 120.2623587000), span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
var body: some View {
VStack{
Map(coordinateRegion: $region)
}
}
}
** 显示大头针与手动缩放:**
import SwiftUI
import MapKit
struct Annotation:Identifiable {
let id = UUID()
let coordinate:CLLocationCoordinate2D
}
struct ContentView: View {
@State private var userTrackingMode:MapUserTrackingMode = MapUserTrackingMode.follow
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 30.2906511800, longitude: 120.2623587000), span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
let annotations = [
Annotation(coordinate: .init(latitude: 30.2926511800, longitude: 120.2623587000)),
Annotation(coordinate: .init(latitude: 30.2826511800, longitude: 120.2623587000)),
Annotation(coordinate: .init(latitude: 30.2726511800, longitude: 120.2623587000)),
]
var body: some View {
VStack{
// Map(coordinateRegion: $region)
Map(coordinateRegion: $region, interactionModes: MapInteractionModes.all, showsUserLocation: true, userTrackingMode: $userTrackingMode, annotationItems: annotations) { annotation in
MapMarker(coordinate: annotation.coordinate, tint: .blue)
}
Button("缩放"){
region.span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
}
}
}
}
网友评论