Driver movement in User Application iOS with Swift 5

Driver movement in User Application iOS with Swift 5
October 25, 2020 12 Comments Development,iOS Development Pushpendra Kumar

Hey, Guys welcome to this tutorial. In this complete video, I will make you understand the quick trick for Driver movement in User Application iOS with Swift 5. Like Uber, Zomato, Swiggy, Ola iOS with Swift 5. And this is a simple way to achieve our goals. Through this simple video tutorial, you can learn more interesting things about the Driver movement in User Application. Like Uber, Zomato, Swiggy, Ola iOS with Swift 5. In this video tutorial, you can learn the basics of the Custom marker, Driver movement in User Application. Like Uber, Zomato, Swiggy, Ola iOS with Swift 5.

And you can create a beautiful iOS application. And also you can get the answer of how to Driver move in User Application. Like Uber, Zomato, Swiggy, Ola iOS with Swift 5. So watch the complete video. And if you watched the complete video then do not forget to give your suggestion in the comment box. Because your feedback & suggestions do matter for me 

Driver movement in User Application iOS

And finally, I would like to say that, If you like this video and it’s helpful for your project then click on the like button and do not forget to subscribe to this YouTube channel for future updates. This tutorial hardly take 1 hour to understand and integrating the complete flow in both iOS application. So first understand from the video and then use the following code.

Grate, I hope You have seen the entire video and You must also like the video. So at the very first I am writing the code for the driver application.

Driver application code for location tracking

//
//  ViewController.swift
//  Driver
//
//  Created by Pushpendra on 24/10/20.
//

import UIKit
import CoreLocation
import Firebase

class ViewController: UIViewController {

    
var dbRef : DatabaseReference?
    
  private  var locationManager : CLLocationManager!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        dbRef = Database.database().reference()
        
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }

}
extension ViewController : CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let currentLocation = manager.location?.coordinate {
            print(currentLocation)
            
            let cordinates = ["latitude" : currentLocation.latitude, "longitude" : currentLocation.longitude]
            
            self.dbRef?.child("ios_driver_points").setValue(cordinates)
        }
    }
}

Grate, I hope you have also follow the video complete video. Now let’s have look on the User Application.

User application code for location tracking

//
//  ViewController.swift
//  UserLocation
//
//  Created by Pushpendra on 24/10/20.
//

import UIKit
import MapKit
import Firebase
import CoreLocation

class ViewController: UIViewController {
    @IBOutlet weak var mapView: MKMapView!
    
    
    private var dbRef : DatabaseReference?
    
    private var driverAnnotation : MKPointAnnotation?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        driverAnnotation = MKPointAnnotation()
        
        dbRef = Database.database().reference()
        
        dbRef?.child("driver_points").observe(.value, with: { (data) in
            let postDict = data.value as? [String : Double] ?? [:]
            let latitude = postDict["latitude"]
            let longitude = postDict["longitude"]
            
            let cordinate = CLLocationCoordinate2D(latitude: latitude!, longitude: longitude!)
            self.setLocation(currentDriverPoints: cordinate)
        })
        
        self.mapView.delegate = self
    }
    
    private var isDriverMarkerSet = false
    
    private func setLocation(currentDriverPoints : CLLocationCoordinate2D){
        print("location : \(currentDriverPoints.latitude), \(currentDriverPoints.longitude)")
        if !isDriverMarkerSet {
            driverAnnotation?.coordinate = currentDriverPoints
            isDriverMarkerSet = true
            mapView.addAnnotation(driverAnnotation!)
        } else {
            let angle = angleFromCoordinate(firstCoordinate: driverAnnotation!.coordinate, secondCoordinate: currentDriverPoints)
            
            UIView.animate(withDuration: 2) {
                self.driverAnnotation?.coordinate = currentDriverPoints
            }
            
            //Getting the MKAnnotationView
            let annotationView = self.mapView.view(for: driverAnnotation!)
            
            //Angle for moving the driver
            UIView.animate(withDuration: 1) {
                annotationView?.transform = CGAffineTransform(rotationAngle: CGFloat(angle))
            }
            
        }
        
        
        self.zoomOnAnnotation()
    }
    
    private func zoomOnAnnotation(){
        let region = MKCoordinateRegion(center: driverAnnotation!.coordinate, latitudinalMeters: 250, longitudinalMeters: 250)
        mapView.setRegion(region, animated: true)
    }
    
    func angleFromCoordinate(firstCoordinate: CLLocationCoordinate2D,
                             secondCoordinate: CLLocationCoordinate2D) -> Double {
        
        let deltaLongitude: Double = secondCoordinate.longitude - firstCoordinate.longitude
        let deltaLatitude: Double = secondCoordinate.latitude - firstCoordinate.latitude
        let angle = (Double.pi * 0.5) - atan(deltaLatitude / deltaLongitude)
        
        if (deltaLongitude > 0) {
            return angle
        } else if (deltaLongitude < 0) {
            return angle + Double.pi
        } else if (deltaLatitude < 0) {
            return Double.pi
        } else {
            return 0.0
        }
    }
}
extension ViewController : MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if !(annotation is MKPointAnnotation) {
            return nil
        }
        let annotationIdentifier = "AnnotationIdentifier"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
        
        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView!.canShowCallout = true
        }
        else {
            annotationView!.annotation = annotation
        }
        let pinImage = UIImage(named: "ic_driver")
        annotationView!.image = pinImage
        return annotationView
    }
    
    
    
    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
        let region = MKCoordinateRegion(center: userLocation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
        self.mapView.setRegion(region, animated: true)
    }
}

Grate, I hope you have done with it.

In Conclusion, You have done with this tutorial. And I hope now the concept of Driver movement in User Application in iOS with Swift 5 is clear. Firstly, If you have any doubts regarding the Driver movement in User Application in iOS with Swift. Then you can comment into the comment box. And If you like this tutorial with a complete description of the simple and attractive tutorial in Android then please like my video on YouTube and do not forget to subscribe to my YouTube channel. Because you will get lots of videos related to android development with very sort tricks.

In addition, If you are more curious about the development and knowing about tutorials then follow the below links 

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.
Leave Comment
  1. 1

    sikis izle

    I your writing style genuinely enjoying this web site. Kaleena Randy Swift

    Reply
  2. 1

    erotik

    Wow! In the end I got a weblog from where I can really obtain useful information concerning my study and knowledge. Jaquelin Geno Zedekiah

    Reply
  3. 1

    erotik

    Great post! You are a very persuasive writer. Nice write up. Idelle Lucas Nonah

    Reply
  4. 1

    erotik

    Good post! We will be linking to this particularly great post on our site. Keep up the good writing. Ariella Hughie Sidwohl

    Reply
  5. 1

    sikis izle

    Admiring the dedication you put into your website and in depth information you provide. Mirilla Brannon Stephie

    Reply
  6. 1

    erotik izle

    I like it whenever people get together and share opinions. Great blog, keep it up. Agnesse Weber Belak

    Reply
  7. 1

    porno

    I think you have remarked some very interesting details , appreciate it for the post. Anselma Armstrong Stafford

    Reply
  8. 1

    sikis izle

    Hello, after reading this awesome paragraph i am also happy to share my knowledge here with colleagues. Tybi Zach Etta

    Reply
  9. 1

    erotik film izle

    Hi there, after reading this remarkable post i am too glad to share my experience here with friends. Cati Lyn Syck

    Reply
  10. 1

    erotik film izle

    The most effective man is usually the grooms most trustworthy and faithful pal or relative. Twila Olvan Vlad

    Reply
  11. 1

    sikis izle

    Good answers in return of this difficulty with firm arguments and explaining the whole thing on the topic of that. Celka Iorgo Tengdin

    Reply
  12. 1

    fabric store

    I would like to thnkx for the efforts you have put in writing this blog. I am hoping the same high-grade site post from you in the upcoming also. In fact your creative writing abilities has inspired me to get my own site now. Actually the blogging is spreading its wings rapidly. Your write up is a great example of it.

    Reply

Leave a reply

Your email address will not be published. Required fields are marked *