import Foundation
import SwiftUI
import MapKit
import Alamofire
struct MapView : UIViewRepresentable {
@EnvironmentObject var viewModule : ViewModule
let map = MKMapView()
func makeUIView(context: Context) -> MKMapView {
map.showsUserLocation = true
map.delegate = context.coordinator
viewModule.GetCoffee {
let p1 = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: map.userLocation.coordinate.latitude, longitude: map.userLocation.coordinate.longitude))
for cafes in viewModule.allCoffee {
let p2 = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: Double(cafes.latitude) ?? 0, longitude: Double(cafes.longitude) ?? 0))
map.addAnnotation(p2)
}
}
return map
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func updateUIView(_ uiView: MKMapView, context: Context) {
}
class Coordinator : NSObject, MKMapViewDelegate {
var control : MapView
var tileRenderer : MKTileOverlayRenderer = MKTileOverlayRenderer(tileOverlay: MKTileOverlay(urlTemplate: ""))
init(_ control: MapView) {
self.control = control
}
func mapViewWillStartLoadingMap(_ mapView: MKMapView) {
print("Map will start loading")
let template = ""
let overlay = MKTileOverlay(urlTemplate: template)
overlay.canReplaceMapContent = true
mapView.addOverlay(overlay, level: .aboveRoads)
self.tileRenderer = MKTileOverlayRenderer(tileOverlay: overlay)
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKTileOverlay {
return tileRenderer
}
else {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = .blue
renderer.lineWidth = 5
return renderer
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
var annotationView = MKAnnotationView()
annotationView.image = UIImage(named: "")
return annotationView
}
else {
var annotationView = MKAnnotationView()
annotationView.image = UIImage(named: "")
return annotationView
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
mapView.overlays.forEach { over in
if !(over is MKTileOverlay) {
mapView.removeOverlay(over)
}
}
AF.request("", method: .get)
.responseDecodable(of: Welcome.self) { res in
debugPrint(res)
var coords : [CLLocationCoordinate2D] = []
var x : [[Double]] = res.value?.routes.first?.geometry.coordinates ?? []
for coord in x {
coords.append(CLLocationCoordinate2DMake(coord[1], coord[0]))
}
var overlay = MKPolyline(coordinates: coords, count: coords.count)
self.control.map.addOverlay(overlay)
}
}
}
}
class LocationManager : NSObject, ObservableObject {
let locationManager = CLLocationManager()
@Published var location : CLLocation? = nil
override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
}
extension LocationManager : CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print(status)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else {
return
}
self.location = location
}
}