【问题标题】:How to show directions in SwiftUI Map()如何在 SwiftUI Map() 中显示方向
【发布时间】:2022-01-05 01:28:35
【问题描述】:

我在 SwiftUI 3 和 Xcode 13+ 中使用 Map


import SwiftUI
import CoreLocation
import CoreLocationUI
import MapKit

Map(coordinateRegion: $mapViewModel.region,
                    interactionModes: [.all],
                    showsUserLocation: true,
                    userTrackingMode: .constant(.none),
                    annotationItems: annotations) { place in
                    MapAnnotation(coordinate: place.placemark.location!.coordinate) {
                        MapPinView()
                    }
                }
                    .tint(.blue)
                    .environmentObject(mapViewModel)
                    .edgesIgnoringSafeArea(.all)
                    .onAppear {
                        mapViewModel.checkLocationServicesIsEnabled()
                        
                    }

所以,我在这里使用 Map(...)

有没有显示两点之间路线的示例?

谢谢

【问题讨论】:

    标签: swiftui apple-maps


    【解决方案1】:

    来自本教程https://www.youtube.com/watch?v=H6pmm62axCg

    你有这里的来源:https://github.com/apatronl/YouTube/blob/main/Directions/Directions/ContentView.swift

    这里是相关代码sn-p:

    let request = MKDirections.Request()
        request.source = MKMapItem(placemark: p1)
        request.destination = MKMapItem(placemark: p2)
        request.transportType = .automobile
    
        let directions = MKDirections(request: request)
        directions.calculate { response, error in
          guard let route = response?.routes.first else { return }
          mapView.addAnnotations([p1, p2])
          mapView.addOverlay(route.polyline)
          mapView.setVisibleMapRect(
            route.polyline.boundingMapRect,
            edgePadding: UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20),
            animated: true)
          self.directions = route.steps.map { $0.instructions }.filter { !$0.isEmpty }
        }
    

    【讨论】:

    猜你喜欢
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2021-10-18
    相关资源
    最近更新 更多