How to open Waze and navigate by address

Asked

Viewed 170 times

1

Good night, you guys

How do I open Waze in the app and set the address via string (I want to navigate directly by address, not by coordinates) Right now, I’m doing it this way:

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 doesn’t open the navigation window... It just sits on the map.

My String with the address is as follows:

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

2 answers

1

If you build using iOS SDK 9.0 and newer versions, you need to add the following code to your app’s Info.plist file 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)")
}

0


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

So, to search in 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!)

for:

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

Also, I could observe that the address you pass sometimes does not find results, as in the example below, where you look:

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 to the following address::

R VISCONDE DE URUGUAI , 311 Niterói

With these changes, you’ll start to get correct results in the integration between your app and Waze.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.