【问题标题】:Xamarin Webview not giving latitude and longitudeXamarin Webview 不提供纬度和经度
【发布时间】:2018-11-01 10:00:44
【问题描述】:

我有一个显示不同位置的纬度和经度数据的网络应用程序。我正在尝试使用在 Xamarin 中使用 Web 视图开发的 android 应用程序打开相同的 Web 应用程序。但是纬度和经度显示为空白。当我使用 Chrome 浏览器时,它工作正常。请帮我。

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.layout3);
    web_view = FindViewById<WebView>(Resource.Id.webview);
    web_view.Settings.JavaScriptEnabled = true;
    web_view.Settings.SetGeolocationEnabled(true);
    web_view.SetWebViewClient(new HelloWebViewClient());
    web_view.SetWebChromeClient(new GeoWebChromeClient());
    web_view.Settings.JavaScriptCanOpenWindowsAutomatically = true;
    web_view.Settings.DisplayZoomControls = true;
    web_view.Settings.JavaScriptEnabled = true;
    web_view.Settings.SetGeolocationEnabled(true);
    web_view.LoadUrl("https://abcd.com");
}


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

}



class HelloWebViewClient:WebViewClient
{
    // For API level 24 and later
    public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
    {
        view.LoadUrl(request.Url.ToString());
        return false;
    }
}

【问题讨论】:

  • 已给出位置权限清单??和 6 及更高版本的运行时..
  • Android Marshmallow 引入了一个全新的应用程序权限旋转,试图不仅简化权限,而且减少所需的权限数量。你可以参考这个博客blog.xamarin.com/…

标签: android xamarin webview


【解决方案1】:

将此添加到清单文件中。

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

在 Activity 中添加这个:然后尝试获取位置值。

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 100;

public boolean checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            //  try again to request the permission.

        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        return false;
    } else {
        return true;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // location-related task you need to do.
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {

                    //Request location updates:
                    locationManager.requestLocationUpdates(provider, 400, 1, this);
                }

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.

            }
            return;
        }

    }
}

【讨论】:

  • @Shahar 对您有帮助吗??
  • 实际上我已经尝试了所有这些仍然对我不起作用。
  • 如果获得许可但 gps 已关闭,那么您将无法获得位置.. 您能确认一下吗?您正在测试哪个 android 版本??
  • 我正在检查 Android 6.0.1 并且位置信息已开启。当我使用 web_view.SetWebChromeClient(new WebChromeClient());像这样它工作正常。但我不想向用户显示 url 和所有内容。
  • 嗨 Jay,我的 Web 应用程序是在 vb.net 中使用 3.5 框架开发的。在该 Web 应用程序中,他们正在使用位置和所有内容。我需要在我的网络视图中显示该网络应用程序。有没有可能?还是任何图书馆问题等?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-09
相关资源
最近更新 更多