2017-02-17 80 views
1

我试图在一个xamarin表单应用程序中启用webview以获取android设备的当前GPS坐标。目前webview /网站将在手机或笔记本电脑上的chrome浏览器中打开时返回GPS坐标,但在应用程序中不会。试图尽可能简化工作并在之后进行扩展。Xamarin Forms WebView地理定位问题

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

<?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> 
+0

您是否配置了您的应用程序以允许使用GPS? – SergioAMG

+0

是的,已经在Android清单中配置了许可,以允许优良和粗略的位置 – Ollie

回答

1

目前在手机上的Chrome浏览器打开时的WebView /网站将返回GPS坐标或一台笔记本电脑,但在应用程序不会。

您需要Droid的项目中使用自定义WebChromeClientWebView。请参阅Android WebView Geolocation

在Xamarin.Forms你可以按照下面的步骤来实现:

  1. 创建PCL项目的WebView自定义控件:

    public class GeoWebView:WebView 
    { 
    } 
    

    而在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> 
    

  2. GeoWebView在Droid的项目像下面创建一个自定义呈现:

    [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); 
         } 
        } 
    } 
    
  3. 权限添加到AndroidManifest.xml

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

然后,你将在网页流量正确地得到你的位置。

0

干杯猫王设法得到一切工作,不得不做出一些细微的变化,以便将发布的一切,我详细做了:

在App.xaml.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Xamarin.Forms; 

namespace WebViewFormsDemo 
{ 
    public partial class App : Application 
    { 
     public App() 
     { 
      InitializeComponent(); 
      MainPage = new MainPage(); 
     } 

    protected override void OnStart() 
    { 
     // Handle when your app starts 
    } 

    protected override void OnSleep() 
    { 
     // Handle when your app sleeps 
    } 

    protected override void OnResume() 
    { 
     // Handle when your app resumes 
    } 
} 

}

在MainPage.xaml确保您的网站是'https'

<?xml version="1.0" encoding="utf-8" ?> 
<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:// --- Your Website ----></local:GeoWebView> 

</ContentPage> 

正如Elvis所说:那么需要在PLC项目中创建一个自定义控件。右键单击并添加新的“Class”来执行此操作。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Xamarin.Forms; 

namespace WebViewFormsDemo 
{ 
    public class GeoWebView : WebView 
    { 
    } 
} 

接下来在Droid类中创建自定义渲染。最初在这里出现了一些错误,主要是缺少'使用'指令以及'汇编'中需要的关键字。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Xamarin.Forms; 
using Xamarin.Forms.Platform.Android; 
using Android.Webkit; 

[assembly: ExportRenderer(typeof(WebViewFormsDemo.GeoWebView), typeof(WebViewFormsDemo.Droid.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); 
     } 
    } 
} 

遵循这些更改一切工作完美。再次感谢猫王!