想要通过 swiftUI 中的按钮在地图上显示放大缩小

发布于 2025-01-17 04:48:32 字数 1971 浏览 1 评论 0原文

我想在 swiftUI Map 中的地图上添加 2 个按钮来放大和缩小地图。我已将屏幕底部的两个按钮放置在 VStack 内的 Stack 中。我需要代码才能使它们工作。

这就是我所拥有的:

import Foundation
import SwiftUI
import MapKit

struct QuakeDetail: View {
    var quake: Quake
    
    @State private var region : MKCoordinateRegion
    init(quake : Quake) {
        self.quake = quake
        _region = State(wrappedValue: MKCoordinateRegion(center: quake.coordinate,
                                                         span: MKCoordinateSpan(latitudeDelta: 3, longitudeDelta: 3)))
    }
    var body: some View {
        ZStack {
            
            VStack {
                Map(coordinateRegion: $region, annotationItems: [quake]) { item in
                    MapMarker(coordinate: item.coordinate, tint: .red)
                } .ignoresSafeArea()
                HStack {
                    Button {
                        print ("pressed +")
                    } label: {
                        HStack {
                            Text("map")
                            Image(systemName: "plus")
                        }
                    }.padding(5).border(Color.blue, width: 1)
                    Spacer()
                    QuakeMagnitude(quake: quake)
                    Spacer()
                    Button {
                        print ("pressed -")
                    } label: {
                        HStack {
                            Text("map")
                            Image(systemName: "minus")
                        }
                    }.padding(5).border(Color.blue, width: 1)
                    
                }.padding()
                Text(quake.place)
                    .font(.headline)
                    .bold()
                Text("\(quake.time.formatted())")
                    .foregroundStyle(Color.secondary)
                Text("\(quake.latitude)   \(quake.longitude)")
                
                
            }
        }
    }    
}

I want to add 2 buttons on a map in swiftUI Map to zoom in and out of it. I have positioned the two buttons at the bottom of the screen in a Stack inside a VStack. I need the code to make them work.

This is what I have:

import Foundation
import SwiftUI
import MapKit

struct QuakeDetail: View {
    var quake: Quake
    
    @State private var region : MKCoordinateRegion
    init(quake : Quake) {
        self.quake = quake
        _region = State(wrappedValue: MKCoordinateRegion(center: quake.coordinate,
                                                         span: MKCoordinateSpan(latitudeDelta: 3, longitudeDelta: 3)))
    }
    var body: some View {
        ZStack {
            
            VStack {
                Map(coordinateRegion: $region, annotationItems: [quake]) { item in
                    MapMarker(coordinate: item.coordinate, tint: .red)
                } .ignoresSafeArea()
                HStack {
                    Button {
                        print ("pressed +")
                    } label: {
                        HStack {
                            Text("map")
                            Image(systemName: "plus")
                        }
                    }.padding(5).border(Color.blue, width: 1)
                    Spacer()
                    QuakeMagnitude(quake: quake)
                    Spacer()
                    Button {
                        print ("pressed -")
                    } label: {
                        HStack {
                            Text("map")
                            Image(systemName: "minus")
                        }
                    }.padding(5).border(Color.blue, width: 1)
                    
                }.padding()
                Text(quake.place)
                    .font(.headline)
                    .bold()
                Text("\(quake.time.formatted())")
                    .foregroundStyle(Color.secondary)
                Text("\(quake.latitude)   \(quake.longitude)")
                
                
            }
        }
    }    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2025-01-24 04:48:32

您必须使用 MKCooperativeRegionspan 参数进行操作(值越小,缩放区域越大),例如(演示缩放 10%):

Button("Zoom In") {
    region.span.latitudeDelta *= 0.9
    region.span.longitudeDelta *= 0.9
}

使用 Xcode 13.2 / iOS 进行测试15.2

You have to operate with span parameter of MKCoordinateRegion for that (the less value the more zoomed area), like (demo zoom on 10%):

Button("Zoom In") {
    region.span.latitudeDelta *= 0.9
    region.span.longitudeDelta *= 0.9
}

Tested with Xcode 13.2 / iOS 15.2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文