0

I have created a WebView App with VS2015 / Xamarin. The WebView loads an external webiste (the pages are not included in the APP). One of the website pages contains an html link of this kind:

<a href="geo:42.374260,-71.120824">

I would like to make sure Android opens Google Maps App when a user clicks on the link. I have made use of ShouldOverrideUrlLoading in this way:

public class WebClient : WebViewClient
        {
            public override bool ShouldOverrideUrlLoading(WebView view, string url)
            {

                //Click on a georeferenced link
                if (url.IndexOf("geo:")>-1)
                {
                    var geoUri = Android.Net.Uri.Parse("geo:42.374260,-71.120824");
                    var mapIntent = new Intent(Intent.ActionView, geoUri);
                    StartActivity(mapIntent);
                    return true;
                }

                //Click on a generic link
                view.LoadUrl(url);
                return true;
            }
        }

Now, problem is, the StartActivity line returns an error: An object reference is required for the non-static field, method, or property 'ContextWrapper.StartActivity(Intent)'

Could you suggest which is the right syntax I should use to avoid this error ?

Antelion
  • 155
  • 2
  • 14

1 Answers1

2

I solved the problem. The right way of calling StartActivity is the following:

 if (url != null && url.IndexOf("geo:")>-1)
                {
                    var geoUri = Android.Net.Uri.Parse(url);
                    var mapIntent = new Intent(Intent.ActionView, geoUri);
                    view.Context.StartActivity(mapIntent);
                    return true;
                }

An interesting thread on the topic is to be found here: How to handle geo: links in webview

Community
  • 1
  • 1
Antelion
  • 155
  • 2
  • 14