SwiftUI Button interact with Map

让人想犯罪 __ 提交于 2020-01-13 19:22:31

问题


I'm totally new with Swift and SwiftUI and for a project group, I need to develop my first IOS app.

I can display a map with Mapbox but I don't know how to follow my user when I click on a button.

I don't know how to interact my button with my struct MapView

This is my code:

MapView.swift:

import Mapbox

struct MapView: UIViewRepresentable {

    let mapView: MGLMapView = MGLMapView(frame: .zero)

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIView(context: UIViewRepresentableContext<MapView>) -> MGLMapView {
        mapView.delegate = context.coordinator
        return mapView
    }

    func updateUIView(_ uiView: MGLMapView, context: UIViewRepresentableContext<MapView>) {

    }

    func styleURL(_ styleURL: URL) -> MapView {
        mapView.styleURL = styleURL
        return self
    }

    func centerCoordinate(_ centerCoordinate: CLLocationCoordinate2D) -> MapView {
        mapView.centerCoordinate = centerCoordinate
        return self
    }

    func zoomLevel(_ zoomLevel: Double) -> MapView {
        mapView.zoomLevel = zoomLevel
        return self
    }

    func userTrackingMode(_ userTrackingMode: MGLUserTrackingMode) -> MapView {
        mapView.userTrackingMode = userTrackingMode
        return self
    }
}

class Coordinator: NSObject, MGLMapViewDelegate {
    var parent: MapView

    init(_ parent: MapView) {
        self.parent = parent
    }
}

ContentView.swift:

import Mapbox

struct ContentView: View {

    @Environment(\.colorScheme) var colorScheme: ColorScheme

    var body: some View {
        ZStack {
            MapView()
                .userTrackingMode(.follow)
                .edgesIgnoringSafeArea(.all)
            HStack(alignment: .top) {
                Spacer()
                VStack() {
                    Button(action: {
                        //ACTION TO CHANGE FOLLOW MODE
                    }) {
                        Image(systemName: "location.fill")
                            .frame(width: 40.0, height: 40.0)
                    }
                    .padding(.top, 60.0)
                    .padding(.trailing, 10.0)
                    .frame(width: 45.0, height: 80.0)
                    Spacer()
                }

            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environment(\.colorScheme, .dark)
    }
}

Thank you a lot !


回答1:


Actually I think in your case, as you have a reference to map, you can try to interact with it directly (aka imperatively, because it is such by nature, so no need to make simple thing complex)

Like

...

let myMapHolder = MapView()
var body: some View {
    ZStack {
        myMapHolder
            .userTrackingMode(.follow)
            .edgesIgnoringSafeArea(.all)
    ...



       VStack() {
            Button(action: {
                self.myMapHolder.mapView.userTrackingMode = _your_mode_
            }) {
                Image(systemName: "location.fill")


来源:https://stackoverflow.com/questions/59414411/swiftui-button-interact-with-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!