Xamarin Forms WebView 地理位置问题 [英] Xamarin Forms WebView Geolocation issue

查看:20
本文介绍了Xamarin Forms WebView 地理位置问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 xamarin 表单应用程序中启用 web 视图,以获取 Android 设备的当前 GPS 坐标.目前,当在手机或笔记本电脑上的 chrome 浏览器中打开时,webview/网站将返回 GPS 坐标,但在应用程序中不会.尝试尽可能简单地使其工作并在之后扩展它.

I'm trying to enable a webview within a xamarin forms app to get the current GPS coordinates of an android device. Currently the webview/website will return the GPS coordinates when opened in a chrome browser on the phone or a laptop, however within the app will not. Trying to get this working as simply as possible and to expand on it after.

到目前为止的代码:XAML 页面:

Code so far: XAML page:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="UITrial.Page2"
             BackgroundColor = "#f0f0ea">
    <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />

  <WebView Source="https://danu6.it.nuigalway.ie/OliverInternetProgramming/project/Loginproject.html" />

</ContentPage>

HTML 页面:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.watchPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";}
    }

function showPosition(position) {
    x.innerHTML="Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
}
</script>

</body>
</html>

推荐答案

目前,当在手机或笔记本电脑上的 chrome 浏览器中打开时,webview/网站将返回 GPS 坐标,但在应用程序中不会.

Currently the webview/website will return the GPS coordinates when opened in a chrome browser on the phone or a laptop, however within the app will not.

您需要在 Droid 项目中为 WebView 使用自定义的 WebChromeClient.请参阅Android WebView 地理位置.

You need to use a custom WebChromeClient for WebView in Droid project. Please refer to Android WebView Geolocation.

在 Xamarin.Forms 中,您可以按照以下步骤完成此操作:

In Xamarin.Forms you can follow the below steps to accomplish this:

  1. 在 PCL 项目中为 WebView 创建自定义控件:

  1. Create a custom Control for WebView in PCL project:

public class GeoWebView:WebView
{
}

并在 Xaml 中使用它:

And use it in Xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:WebViewFormsDemo"
         x:Class="WebViewFormsDemo.MainPage">
<local:GeoWebView 
    Source="https://danu6.it.nuigalway.ie/OliverInternetProgramming/project/Loginproject.html"></local:GeoWebView>

在 Droid 项目中为 GeoWebView 创建自定义渲染器,如下所示:

Create a Custom Renderer for GeoWebView in Droid Project like below:

[assembly:ExportRenderer(typeof(GeoWebView),typeof(GeoWebViewRenderer))]
namespace WebViewFormsDemo.Droid
{
    public class GeoWebViewRenderer:WebViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);
            Control.Settings.JavaScriptEnabled = true;
            Control.SetWebChromeClient(new MyWebClient());
        }
    }

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

  • AndroidManifest.xml 添加权限:

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

  • 然后您将在 webview 中正确获取您的位置.

    Then you will get your location in webview correctly.

    这篇关于Xamarin Forms WebView 地理位置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆