2017-02-17 88 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); 
     } 
    } 
} 

遵循這些更改一切工作完美。再次感謝貓王!