Skip to content

Commit

Permalink
Merge pull request #6 from LucasJorgeHubert/feature/creating-routes
Browse files Browse the repository at this point in the history
Feature - Create routes render
  • Loading branch information
LucasJorgeHubert authored May 18, 2023
2 parents d4f08f5 + 4abc50e commit 15c629b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion eUberUI/Core/Home/View/MapViewActionButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct MapViewActionButton: View {
: "line.3.horizontal"
)
.font(.title2)
.foregroundColor(.black)
.foregroundColor(Color.yellow)
.padding()
.background(.white)
.clipShape(Circle())
Expand Down
43 changes: 42 additions & 1 deletion eUberUI/Core/Home/View/UberMapViewRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ struct UberMapViewRepresentable: UIViewRepresentable {
return mapView
}



func updateUIView(_ uiView: UIViewType, context: Context) {
if let coordinate = locationViewModel.selectedLocationCoordinate {
print("DEBUG: Selected coordinate in map view \(coordinate)")
context.coordinator.addAndSelectAnnotation(withCoordinate: coordinate)
context.coordinator.configurePolyline(withDestinationCoordinate: coordinate)
}
}

Expand All @@ -40,6 +43,7 @@ extension UberMapViewRepresentable {

// MARK: - Properties
let parent: UberMapViewRepresentable
var userLocationCoordinate: CLLocationCoordinate2D?

// MARK: - Lifecycle

Expand All @@ -51,17 +55,25 @@ extension UberMapViewRepresentable {
// MARK: - MKMapViewDelegate

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
self.userLocationCoordinate = userLocation.coordinate
let region = MKCoordinateRegion(
center: CLLocationCoordinate2D(
latitude: userLocation.coordinate.latitude,
longitude: userLocation.coordinate.longitude
),
span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
span: MKCoordinateSpan(latitudeDelta: 0.10, longitudeDelta: 0.10)
)

parent.mapView.setRegion(region, animated: true)
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let polyline = MKPolylineRenderer(overlay: overlay)
polyline.strokeColor = .systemYellow
polyline.lineWidth = 6
return polyline
}

// MARK: - Helpers

func addAndSelectAnnotation(withCoordinate coordinate: CLLocationCoordinate2D) {
Expand All @@ -73,5 +85,34 @@ extension UberMapViewRepresentable {

parent.mapView.showAnnotations(parent.mapView.annotations, animated: true)
}

func configurePolyline(withDestinationCoordinate coordinate: CLLocationCoordinate2D) {
guard let userLocationCoordinate = self.userLocationCoordinate else { return }
getDestinationRoute(from: userLocationCoordinate, to: coordinate) { route in
self.parent.mapView.addOverlay(route.polyline)
}
}

func getDestinationRoute(
from userLocation: CLLocationCoordinate2D,
to destination: CLLocationCoordinate2D,
completion: @escaping(MKRoute) -> Void
) {
let userPlacemark = MKPlacemark(coordinate: userLocation)
let userDestination = MKPlacemark(coordinate: destination)
let request = MKDirections.Request()
request.source = MKMapItem(placemark: userPlacemark)
request.destination = MKMapItem(placemark: userDestination)
let direction = MKDirections(request: request)

direction.calculate { response, error in
if let error = error {
print("DEBUG: Failed to get direction with error \(error.localizedDescription)")
}

guard let route = response?.routes.first else { return }
completion(route)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct LocationSearchActivationView: View {
var body: some View {
HStack {
Rectangle()
.fill(Color.black)
.fill(Color(.systemYellow))
.frame(width: 8, height: 8)
.padding(.horizontal)

Expand All @@ -24,7 +24,7 @@ struct LocationSearchActivationView: View {
.background(
Rectangle()
.fill(Color.white)
.shadow(color: .black, radius: 6)
.shadow(color: Color(.systemYellow), radius: 6)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ struct LocationSearchResultCell: View {
HStack {
Image(systemName: "mappin.circle.fill")
.resizable()
.foregroundColor(.blue)
.accentColor(.white)
.foregroundColor(.yellow)
.accentColor(Color(.systemYellow))
.frame(width: 40, height: 40)

VStack(alignment: .leading) {
Expand Down
8 changes: 5 additions & 3 deletions eUberUI/Core/LocationSearch/View/LocationSearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ struct LocationSearchView: View {
HStack {
VStack {
Circle()
.fill(Color(.systemGray3))
.fill(Color(.systemYellow))
.frame(width: 6, height: 6)
.opacity(0.8)

Rectangle()
.fill(Color(.systemGray3))
.fill(Color(.systemYellow))
.frame(width: 1, height: 24)
.opacity(0.6)

Rectangle()
.fill(Color.black)
.fill(Color(.systemYellow))
.frame(width: 6, height: 6)
}

Expand Down

0 comments on commit 15c629b

Please sign in to comment.