首页 文章

使用Xamarin在Android WebView中进行地理定位

提问于
浏览
2

所以我_1075461已经使用这个资源作为开始:http://turbomanage.wordpress.com/2012/04/23/how-to-enable-geolocation-in-a-webview-android/并且我很好地复制了C#中所需的组件,但是在WebView中加载的页面只是在地理定位提示处于待处理状态时加载的灰色页面(不允许)也没有否认) .

我有所需的权限:

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

这是我的webView用法

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");

自定义类

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;
            }
        }

有没有人有什么建议?

谢谢

2 回答

  • 2

    您在代码中缺少 override 关键字:

    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;
            }
         }
    
  • 0

    如果您在桌面浏览器上加载此页面,

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

    您只会看到您描述的灰色页面 .

    你必须设置纬度(44.433106)和经度(26.103687)的值,这里有两个例子:

    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");
    

    更多信息:Google Maps Android API v2

相关问题