【问题标题】:Problem of resizing Map using SwiftUI Mapkit使用 SwiftUI Mapkit 调整地图大小的问题
【发布时间】:2021-04-29 19:14:34
【问题描述】:

我想在应用程序上放大我的地图,我尝试使用latitudinalMeters: 300longitudinalMeters: 300 或使用latitudeDelta: 0.001 旋转。他们两个都没有工作。

我也选择了(0, 0)作为我的中心,但是每次我在模拟器上运行时,我都以(37.326010,-122.026056)作为我的中心。显然,我在位置管理器中设置的中心和区域的默认设置在 ContentView 中都不起作用。

这是我的 LocationManager.swift 代码:

import Foundation
import CoreLocation
import MapKit

class LocationManager: NSObject, ObservableObject{
    let locationManager = CLLocationManager()
    
    @Published var location: CLLocation?
    @Published var region: MKCoordinateRegion
    
    override init(){
        self.region = MKCoordinateRegion(center: CLLocationCoordinate2D.init(latitude: 0,longitude: 0),latitudinalMeters: 300, longitudinalMeters: 300)
        super.init()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
    }
}


extension LocationManager : CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager,
                         didUpdateLocations locations: [CLLocation]){
        guard let location = locations.last else { return }
        self.region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 300, longitudinalMeters: 300)
        self.location = location
    }
}

这是我的 ContentView

struct ContentView: View {
    var body: some View {
        MapView2()
    }
}

struct MapView2: View {
    
    @ObservedObject var locationManager = LocationManager()
    
    var body: some View {
        let coord = locationManager.location?.coordinate
        let lat = coord?.latitude ?? 0
        let lon = coord?.longitude ?? 0
        return VStack {
            Map(coordinateRegion: $locationManager.region,
                interactionModes: .all,
                showsUserLocation: true, userTrackingMode: .constant(.follow))
        }
    }
}

【问题讨论】:

    标签: swiftui mapkit


    【解决方案1】:

    至于使用 MapKit 的 SwiftUI,我不会使用 CoreLocation 框架。您可以使用.onChange 修饰符对您的视图执行缩放更改。如果需要,您可以使用 @State var zoom 和 SwiftUI 手势来执行它们,或者任何可以实时进行这些更改的东西。我在滑块中添加了两个按钮来放大或缩小示例。

    import SwiftUI
    import MapKit
    
    struct ContentView: View {
      
      var body: some View {
        MapsView()
      }
    }
    
    struct MapsView: View {
    
      @State var zoom: CGFloat = 15
    
      @State var mapCoordinate = MKCoordinateRegion(
        center: CLLocationCoordinate2D(
          latitude: 38.989202809314854,
          longitude: -76.93626224283602),
        span: MKCoordinateSpan(
          latitudeDelta: .zero,
          longitudeDelta: .zero))
    
      var body: some View {
        VStack(spacing: 16) {
    
          Map(coordinateRegion: $mapCoordinate)
            .ignoresSafeArea(edges: .all)
    
          // You can see the changes being operating by the .onChange modifier.
          Slider(value: $zoom,
                 in: 0.01...50,
                 minimumValueLabel: Image(systemName: "plus.circle"),
                 maximumValueLabel: Image(systemName: "minus.circle"), label: {})
            .padding(.horizontal)
            .onChange(of: zoom) { value in
              mapCoordinate.span.latitudeDelta = CLLocationDegrees(value)
              mapCoordinate.span.longitudeDelta = CLLocationDegrees(value)
            }
        }
        .font(.title)
      }
    }
    

    【讨论】:

    • 您的代码运行良好,但是当我想通过在地图中添加参数来跟踪用户的位置时:Map(coordinateRegion: $mapCoordinate, interactionModes: .all, showsUserLocation: true, userTrackingMode: .constant(.follow)) .ignoresSafeArea(edges: .all) 无论我如何滚动条,缩放级别都会保持不变,这也是关键我的问题点,我该如何解决这个问题,以便在跟踪期间可以更改缩放级别?谢谢!!
    • 您可以在userTrackingMode 参数中声明此wrapper@State var userTrackingMode: MapUserTrackingMode = .follow 并将其添加到userTrackingMode 参数中,如下所示:$userTrackingMode。更详细地说,您是在真实设备上而不是在模拟器上测试您的位置吗?
    • 包装器值起作用,您能否解释一下为什么以及如何起作用,而不是使用常量内部参数?我用的是模拟器,我做的是城市跑,我还没试过在真机上跑
    • 由于在结构上设置的视图,SwiftUI 需要在值更新时重绘整个结构。包装器值用于告诉 SwiftUI 视图其更改。发生这种情况时,在此示例中,MapsView 将重绘自身,您的视图将相应地更新为新值。使用常量时,对于 SwiftUI 视图,该值永远不会改变,因此不需要重新绘制有关该值的视图。当视图中的值永远不会改变时使用常量,或者您可以在预览中使用它来填充假数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多