使用SwiftUI将单个图钉添加到Mapkit [英] Add single pin to Mapkit with SwiftUI
本文介绍了使用SwiftUI将单个图钉添加到Mapkit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用Xcode 11 GM/SwiftUI在地图上添加简单的图钉.
How can I add a simple pin on my map with Xcode 11 GM / SwiftUI.
我的代码如下(这里显示了以坐标为中心的地图),但是我只想在其中显示其他坐标的一针.
My code is as follows (here it shows me the map centered with the coordinates) but I want to show there only one pin of other coordinates.
import SwiftUI
import MapKit
struct ContentView: UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
MKMapView(frame: .zero)
}
func updateUIView(_ view: MKMapView, context: Context) {
let coordinate = CLLocationCoordinate2D(
latitude: 34.011_286, longitude: -116.166_868)
let span = MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)
let region = MKCoordinateRegion(center: coordinate, span: span)
view.setRegion(region, animated: true)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
谢谢您的任何建议.
推荐答案
更新您的代码:
struct ContentView: UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
MKMapView(frame: .zero)
}
func updateUIView(_ view: MKMapView, context: Context) {
// 1
view.mapType = MKMapType.standard // (satellite)
// 2
let mylocation = CLLocationCoordinate2D(latitude: -6.863190,longitude: -79.818250)
// 3
let coordinate = CLLocationCoordinate2D(
latitude: -6.864138, longitude: -79.819634)
let span = MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
let region = MKCoordinateRegion(center: coordinate, span: span)
view.setRegion(region, animated: true)
// 4
let annotation = MKPointAnnotation()
annotation.coordinate = mylocation
annotation.title = "My Location"
annotation.subtitle = "Visit us soon"
view.addAnnotation(annotation)
}
}
这篇关于使用SwiftUI将单个图钉添加到Mapkit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文