Search code examples
iosarraysswiftcoordinateswaze

Set Custom Coordinates to a function, Waze Integration


I'm trying to implement navigation using Waze in my App, using their own API: here.

I want to set in custom coordinates that are set in an array and then fit them in this code:

func navigate(toLatitude latitude: Double , longitude: Double) {
                if UIApplication.shared.canOpenURL(URL(string: "waze://")!) {
                    // Waze is installed. Launch Waze and start navigation
                    let urlStr: String = "waze://?ll=\(latitude),\(longitude)&navigate=yes"
                    UIApplication.shared.openURL(URL(string: urlStr)!)
                }
                else {
                    // Waze is not installed. Launch AppStore to install Waze app
                    UIApplication.shared.openURL(URL(string: "http://itunes.apple.com/us/app/id323229106")!)
                }
            }

i have tried settings up different type of arrays but didn't succeed to make it work . So if you could help me out set custom array holding latitude and longitude that would work properly with the code, that would be awesome

your help will be very much helpful,

Thank you in advance.


Solution

  • Waze app supporting only destination latitude and longitude.

    waze://?ll=<lat>,<lon>&navigate=yes

    CLLocationCordiates will expect two parameters latitude and longitude, which is CLLocationDegrees type. CLLocationDegrees is nothing but Double value.

    let location =  CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    

    If you have double values then no need to construct as CLLocationCoordinate2D. You can use the same function -> navigate() which you mentioned in your question

    Pass the value to a below function to open Waze app.

    func openWaze(location : CLLocationCoordinate2D) {
        if UIApplication.shared.canOpenURL(URL(string: "waze://")!) {
            // Waze is installed. Launch Waze and start navigation
            let urlStr: String = "waze://?ll=\(location.latitude),\(location.longitude)&navigate=yes"
            UIApplication.shared.openURL(URL(string: urlStr)!)
        }
        else {
            // Waze is not installed. Launch AppStore to install Waze app
            UIApplication.shared.openURL(URL(string: "http://itunes.apple.com/us/app/id323229106")!)
        }
    }