Location

PHOTO EMBED

Wed Aug 09 2023 06:34:24 GMT+0000 (Coordinated Universal Time)

Saved by @hasnat #ios #swift #location

//MARK: - Location


 var locationManager = CLLocationManager()
    var currentLatitude = ""
    var currentLongitude = ""
    var cityName = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        locationManager.delegate = self
        
        getLocation()
    }

extension LocationViewController: CLLocationManagerDelegate {
    
    func navigateToMaps() {
        
        let address = bookingModel.addressModel
        let lat = address["locationLat"] as? String ?? ""
        let longitude = address["locationLng"] as? String ?? ""
        //let title = address["location"] as? String ?? ""
        
        let destinationLatitude = Double(longitude) ?? 0.0 // Replace with the destination latitude
        let destinationLongitude: Double = Double(lat) ?? 0.0 // Replace with the destination longitude
        
        if UIApplication.shared.canOpenURL(URL(string: "http://maps.apple.com/")!) {
            // Open Apple Maps with the specified source and destination coordinates
            let mapsURL = URL(string: "http://maps.apple.com/?saddr=\(Double(currentLatitude) ?? 0.0),\(Double(currentLongitude) ?? 0.0)&daddr=\(destinationLatitude),\(destinationLongitude)")!
            UIApplication.shared.open(mapsURL, options: [:], completionHandler: nil)
        } else {
            // Handle the case where Apple Maps app is not available
            print("Apple Maps is not available")
        }
    }
    
    func getLocation () {
        
        if #available(iOS 14.0, *) {
            switch locationManager.authorizationStatus {
            case .notDetermined:
                locationManager.requestWhenInUseAuthorization()
                locationManager.requestLocation()
                break
            case .authorizedWhenInUse :
                locationManager.requestWhenInUseAuthorization()
                locationManager.requestLocation()
                break
                
            default:
                print("status : ", locationManager.authorizationStatus)
                break
            }
        } else {
            print("Error894")
            // Fallback on earlier versions
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("Got Location Data")
        if let location = locations.last {
            let lat = String(location.coordinate.latitude)
            let lng = String(location.coordinate.longitude)
            
            print("Lat : ", lat)
            print("lng : ", lng)
            
            getAddressFromLatLon(pdblLatitude: String(location.coordinate.latitude), withLongitude: String(location.coordinate.longitude))
        } else {
            print("error 120")
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error121 : ", error)
    }
    
    func getAddressFromLatLon(pdblLatitude: String, withLongitude pdblLongitude: String) {
        // SVProgressHUD.show()
        var center : CLLocationCoordinate2D = CLLocationCoordinate2D()
        let lat: Double = Double("\(pdblLatitude)")!
        //21.228124
        
        let lon: Double = Double("\(pdblLongitude)")!
        //72.833770
        let ceo: CLGeocoder = CLGeocoder()
        center.latitude = lat
        center.longitude = lon
        
        let loc: CLLocation = CLLocation(latitude:center.latitude, longitude: center.longitude)
        
        ceo.reverseGeocodeLocation(loc, completionHandler:
                                    {(placemarks, error) in
            if (error != nil)
            {
                print("reverse geodcode fail: \(error!.localizedDescription)")
                SVProgressHUD.dismiss()
                return
            }
            let pm = placemarks! as [CLPlacemark]
            
            for p in pm {
                print(" p :: ", p)
            }
            
            if pm.count > 0 {
                let pm = placemarks![0]
                let cityName = pm.locality ?? ""
                let timeZone : String = pm.timeZone?.identifier ?? ""
                
                print("timeZone : ", timeZone)
                print("country : ",pm.country ?? "")
                print("city : ", cityName)
                print("locLat : ", pdblLatitude)
                print("locLng : ", pdblLongitude)
                self.currentLatitude = pdblLatitude
                self.currentLongitude = pdblLongitude
                self.cityName = cityName
                self.getBookingCoordinates()
            }
            SVProgressHUD.dismiss()
        })
    }
}
content_copyCOPY