gpt4 book ai didi

ios - 使用 SwiftUI 将单个图钉添加到 Mapkit

转载 作者:行者123 更新时间:2023-11-29 11:26:49 25 4
gpt4 key购买 nike

如何使用 Xcode 11 GM/SwiftUI 在我的 map 上添加一个简单的图钉。

我的代码如下(这里它显示了以坐标为中心的 map )但我只想显示其他坐标的一个图钉。

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)






}
}

Preview

关于ios - 使用 SwiftUI 将单个图钉添加到 Mapkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57961576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com