user3158161
user3158161

Reputation: 91

Geolocation in Android WebView using Xamarin

So I'm trying to enable Geolocation on a Webview using Xamarin. I've been using this resource as a start: http://turbomanage.wordpress.com/2012/04/23/how-to-enable-geolocation-in-a-webview-android/ and I've pretty well duplicated the needed components in C#, but the page that loads in the WebView is just the gray page that loads when the geolocation prompt is pending (neither allowed nor denied).

I have the permissions needed:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

Here's my webView usage

webvw = FindViewById<WebView>(Resource.Id.webView1);
webvw.SetWebViewClient(new GeoWebViewClient());
webvw.SetWebChromeClient(new GeoWebChromeClient());
webvw.Settings.JavaScriptCanOpenWindowsAutomatically = true;
webvw.Settings.DisplayZoomControls = true;
webvw.Settings.JavaScriptEnabled = true;
webvw.Settings.SetGeolocationEnabled(true);
webvw.LoadUrl("https://google-developers.appspot.com/maps/documentation/javascript/examples/full/map-geolocation");

The custom classes

 public class GeoWebChromeClient : WebChromeClient
        {
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.ICallback callback)
            {
                // Always grant permission since the app itself requires location
                // permission and the user has therefore already granted it
                callback.Invoke(origin, false, false);
            }
        }


        public class GeoWebViewClient : WebViewClient
        {
            public bool shouldOverrideUrlLoading(WebView view, string url)
            {
                // When user clicks a hyperlink, load in the existing WebView
                view.LoadUrl(url);
                return true;
            }
        }

Does anyone have any suggestions?

Thanks

Upvotes: 2

Views: 2269

Answers (2)

Jon Douglas
Jon Douglas

Reputation: 13176

You are missing the override keyword in your code:

    public class GeoWebChromeClient : WebChromeClient
    {
        public override void OnGeolocationPermissionsShowPrompt(string origin, GeolocationPermissions.ICallback callback)
        {
            callback.Invoke(origin, false, false);
        }

    }


    public class GeoWebViewClient : WebViewClient
    {
        public override bool ShouldOverrideUrlLoading(WebView view, string url)
        {
            view.LoadUrl(url);
            return true;
        }
     }

Upvotes: 2

Jorgesys
Jorgesys

Reputation: 126523

If you load this page on your Desktop browser,

https://google-developers.appspot.com/maps/documentation/javascript/examples/full/map-geolocation

you only will the the gray page that you describe.

You have to set values of latitude (44.433106) and longitude (26.103687), here are two examples:

webvw.LoadUrl("http://maps.google.com/maps?q=44.433106,26.103687(Jorgesys @ Bucharest!)&iwloc=A&hl=en");


webvw.LoadUrl("http://maps.googleapis.com/maps/api/streetview?size=1000x1000&location=44.433106,26.103687&fov=90&heading=235&pitch=10&sensor=false");

More info: Google Maps Android API v2

Upvotes: 0

Related Questions