4

I'm developing an App using Xamarin Forms (cross platform), and im trying to open Waze app from my app, passing latitude and longitude.
It opens Waze very well, but Waze just open, it didn't try to find the address or the latitude/longitude that I passed.

string stringUri = @"waze://ul?ll=" + client.Latitude + ","  + client.Longitude + "&navigate=yes";
Intent intent = new Intent(Intent.ActionView);
intent.AddFlags(ActivityFlags.NewTask);
intent.SetData(Android.Net.Uri.Parse(stringUri));
Android.App.Application.Context.StartActivity(intent);

Some Ideas on how I make it work?

---EDIT---
Finally, it WORKED, using the idea from @SushiHangover, i managed to achieve the desired result. The final code is here:

public static Task < bool > OpenWazeAndroid(decimal latitude, decimal longitude, string address) {
    var lat = latitude.ToString().Replace(",", ".");
    var longi = longitude.ToString().Replace(",", ".");
    const string wazePrefix = "waze://";
    Android.Content.Intent intent = new Android.Content.Intent(Android.Content.Intent.ActionView, Android.Net.Uri.Parse(wazePrefix));
    string wazeURL = ("https://waze.com/ul?q=" + address + "&ll=" + lat + "," + longi + "&z=8&navigate=yes");
    wazeURL = wazeURL.Replace(" ", "%20");
    var resolveInfo = Android.App.Application.Context.PackageManager.ResolveActivi‌​ ty(intent, 0);
    Android.Net.Uri wazeUri;
    if (resolveInfo != null) {
        wazeUri = Android.Net.Uri.Parse(wazeURL);
    } else {
        wazeUri = Android.Net.Uri.Parse("market://details?id=com.waze");
    }
    intent.AddFlags(Android.Content.ActivityFlags.NewTask);
    intent.SetData(wazeUri);
    Android.App.Application.Context.StartActivity(intent);

    return Task.FromResult(true);
}
Calvin Nunes
  • 6,376
  • 4
  • 20
  • 48
  • That looks like the correct way, assuming that `client.Latitude` and `Longitude` return good values. Does Waze behave if you open with, for instance, "https://waze.com/ul?q=Hawaii" ? – Larry OBrien Nov 17 '17 at 23:53
  • I also tried with addresses, it had the same behavior, just open app, not looking for the place. – Calvin Nunes Nov 20 '17 at 10:33

1 Answers1

3

The direct app link on Android does not respect the link properties/options (Waze iOS does), so use the web base url (https://waze.com) to properly open Waze with the deep link options.

Example:

const string wazeAppURL = "waze://";
var wazeURL = $"https://waze.com/ul?ll={loc[0]},{loc[1]}&navigate=yes";
var intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse(wazeAppURL));
var resolveInfo = PackageManager.ResolveActivity(intent, 0);
var wazeUri = resolveInfo != null ? Android.Net.Uri.Parse(wazeURL) : Android.Net.Uri.Parse("market://details?id=com.waze");
intent.SetData(wazeUri);
StartActivity(intent);
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • If a use https, it opens the browser, not the app... I'll try again, but at least for me, it was not working to open the app – Calvin Nunes Nov 20 '17 at 10:32
  • Waze registers Waze and https since it supports deep links, IF the user choices the browser vs. the Waze app then the browser opens, goes to Waze where the user is sent back to the app. – SushiHangover Nov 20 '17 at 10:41
  • good! I'm trying here, but it's showming an error in "var resolveInfo = PackageManager.ResolveActivity(intent, 0);" what is this part about? – Calvin Nunes Nov 20 '17 at 10:49
  • I need this function to open the app inside a static class (it's a library of functions that can be called in every part of the App), and package manager is abstract and non-static. So I'm looking a way to solve this, then I'll check if the waze opens. – Calvin Nunes Nov 20 '17 at 11:23
  • I have the xaml.cs class, that is the View (xamarin Forms), when user is using android and select to open Waze, then it should call this function from my android functions library that i've created. The entire library is static. the error it is showing me is: "An object reference is required to access non-static field member or property 'PackageManager.ResolveActivity" – Calvin Nunes Nov 20 '17 at 11:44
  • I really didn't find a way to use the PackageManager.resolveActivity, even in a non-static method it keep showing this error... i'll try to remove that part and test. thanks anyway – Calvin Nunes Nov 20 '17 at 13:18
  • I was trying to use 'Android.Content.PM.PackageMa....' now, it worked, no errors. BUT, Waze just opened again, not searched for lat and long... i really need to use the code as {loc[0]},{loc[1]} ? can't be variables? I edited my question – Calvin Nunes Nov 21 '17 at 13:27
  • @CalvinNunes No, that is what my code used loc[0] is the lat and loc[1] is the lng, you can use what lat/lng variables you have.... – SushiHangover Nov 21 '17 at 13:54