How to open Waze and browse by address

Good night, folks

How do I open Waze in the app and set the address via string (I want to navigate directly by address, not by coordinates) At the moment, I am doing as follows:

func showWazeNaviationWithUrl(_ strURL: String) {
    if let url = URL(string: "waze://") {
        if UIApplication.shared.canOpenURL(url) {
            if let url = URL(string: strURL) {
                UIApplication.shared.openURL(url)
            }
        } else {
            //Waze is not installed. Launch AppStore to install Waze app
            if let url = URL.init(string: "https://itunes.apple.com/app/id323229106") {
                UIApplication.shared.openURL(url)
            }
        }

In another class that fires this method, I have the following:

   func startNavigation(address: String) {

        var allowedQueryParamAndKey = NSCharacterSet.urlQueryAllowed
        allowedQueryParamAndKey.remove(charactersIn: ";/?:@&=+$, ")
        let _address = address.addingPercentEncoding(withAllowedCharacters: allowedQueryParamAndKey)

        let strURL = String(format: "https://waze.com/ul?ll=%@&navigate=yes", _address!)
        self.delegate?.showWazeNaviationWithUrl(strURL)
    }

After all this is executed, the app opens however, it does not open the navigation window... It just stands still on the map.

My string with the address is as follows shape:

"https://waze.com/ul?ll=R%20VISCONDE%20DE%20URUGUAI%20%2C%20311%20%20-%20CENTRONiter%C3%B3i%20-%20RJ&navigate=yes"
Author: Bruno Vavretchek, 2019-02-18

2 answers

Basically, your query URL is wrong. Looking at the documentation of Waze , you can verify that you are using the parameter ?ll=... instead of the ?q=..., that is, you are looking through a parameter of Latitude and Longitude for an address.

Thus, to search the correct parameter (an address in an address), you must replace the following line:

let strURL = String(format: "https://waze.com/ul?ll=%@&navigate=yes", _address!)

By:

let strURL = String(format: "https://waze.com/ul?q=%@&navigate=yes", _address!)

Also, I could observe that the address you pass as times does not find a result, as in the example below, where you search for:

R VISCONDE DE URUGUAI , 311  - CENTRONiterói - RJ

If you check, the neighborhood and City are together, causing no results to appear. The test I did was changing the URL to the parameter ?q= and with the following address:

R VISCONDE DE URUGUAI , 311 Niterói

With these changes, you will begin to have correct results in the integration between your application and Waze.

 0
Author: Lucas Eduardo, 2019-02-19 14:20:26

If you build using iOS SDK 9.0 and newer, you will need to add the following code to the Info file.plist of your app to include Waze:

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>waze</string>
</array>

The following code example will navigate to latitude / longitude if Waze is installed. Otherwise, the Apple app store will open for the user to install the App:

- (void) navigateToLatitude:(double)latitude longitude:(double)longitude
{
  if ([[UIApplication sharedApplication]
    canOpenURL:[NSURL URLWithString:@"waze://"]]) {
      // O Waze está instalado. Inicia o Waze e a navegação com base na latitude e longitude
      NSString *urlStr =
        [NSString stringWithFormat:@"https://waze.com/ul?ll=%f,%f&navigate=yes",
        latitude, longitude];
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
  } else {
    // O Waze não está instalado. Inicia a AppStore para instalar o Waze
    [[UIApplication sharedApplication] openURL:[NSURL
      URLWithString:@"http://itunes.apple.com/us/app/id323229106"]];
  }
}

If you need to convert an address to latitude and longitude, use CoreLocation as in the example below:

import CoreLocation

var geocoder = CLGeocoder()
geocoder.geocodeAddressString("Seu endereço") {
    placemarks, error in
    let placemark = placemarks?.first
    let latitude = placemark?.location?.coordinate.latitude
    let longitude = placemark?.location?.coordinate.longitude
    print("Lat: \(latitude), Lon: \(longitude)")
}
 1
Author: Leonardo Figueiredo, 2019-02-21 13:21:58