4

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.

bajocode
  • 260
  • 2
  • 10
RandomGeek
  • 303
  • 5
  • 20

3 Answers3

9

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")!)
    }
}
Subramanian P
  • 4,365
  • 2
  • 21
  • 25
  • Hey for some reason the action is not being called.... which is weird ... check this : https://stackoverflow.com/questions/44999453/alertviewcontroller-button-function-not-working – RandomGeek Jul 09 '17 at 18:03
  • Thanks! with your help and other i got this thing working properly....Thank a lot man :) – RandomGeek Jul 09 '17 at 23:35
5

When compiling with iOS SDK 9.0 and later, you must update your application's property list file with the following to include Waze:

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>waze</string>
</array>
Kawe
  • 659
  • 8
  • 18
0

To Create custom array for holding lat and long :

NSMutableArray *latLongArray = [[NSMutableArray alloc]init];
[latLongArray addObject :[NSDictionary dictionaryWithObjectsAndKeys:lat,@"latitude",long,@"longitude",nil]];
[latLongArray addObject :[NSDictionary dictionaryWithObjectsAndKeys:lat,@"latitude",long,@"longitude",nil]];
[latLongArray addObject :[NSDictionary dictionaryWithObjectsAndKeys:lat,@"latitude",long,@"longitude",nil]];

Substitute lat long values.

Shailesh
  • 51
  • 7