我正在开发一个新的SwiftUI应用程序,我试图弄清楚如何使这个Swift项目与SwiftUI兼容。
问题是我无法使UIViewRepresentable工作。我得到一个错误:
类型“方向图”不符合协议“UIViewRepresentable”
下面是我的代码:
import SwiftUI
import MapKit
struct DirectionsMap: UIViewRepresentable {
@EnvironmentObject var model: ViewModel
var business: Businesses
// Start Coordinates for Directions
var start: CLLocationCoordinate2D {
return model.locationManager.location?.coordinate ?? CLLocationCoordinate2D()
}
// End Coordinates for Directions
var end: CLLocationCoordinate2D {
if let lat = business.coordinates?.latitude, let long = business.coordinates?.longitude {
return CLLocationCoordinate2D(latitude: lat, longitude: long)
}
else {
return CLLocationCoordinate2D()
}
}
func makeUIView(context: Context) -> MKMapView {
// Create Map
let map = MKMapView()
map.delegate = context.coordinator
map.showsUserLocation = true
map.userTrackingMode = .followWithHeading
// Create Directions request
var request = MKDirections.Request()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: start))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: end))
// Create Directions Object
let directions = MKDirections(request: request)
directions.calculate { response, error in
if error == nil && response != nil {
for route in response!.routes {
map.addOverlay(route.polyline)
map.setVisibleMapRect(route.polyline.boundingMapRect, edgePadding: UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100), animated: true)
}
}
}
let annotation = MKPointAnnotation()
annotation.coordinate = end
annotation.title = business.name ?? ""
map.addAnnotation(annotation)
return map
}
func updateUIView(_ uiView: MKMapView, context: Context) {
}
static func dismantleUIView(_ uiView: MKMapView, coordinator: ()) {
uiView.removeAnnotations(uiView.annotations)
uiView.removeOverlays(uiView.overlays)
}
//MARK: - Coordinator
func makeCoordinator() -> Coordinator {
return Coordinator()
}
}
class Coordinator: NSObject, MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = .blue
renderer.lineWidth = 5
return renderer
}
}
1条答案
按热度按时间vvppvyoh1#
尝试添加
我注意到,当改变make和update函数的类型时,协议需要显式的typealias。