gpt4 book ai didi

javascript - WinForms 应用程序中的地理定位

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:03:09 24 4
gpt4 key购买 nike

我想创建一个 WinForms 应用程序,它可以像使用 javascript 函数 navigator.geolocation.getCurrentPosition(showPosition);

的网络浏览器一样检测位置

有没有什么方法可以直接在 WinForms 中执行此操作?

我认为我可以使用 WebBrowser 控件来执行此操作,但我认为它不支持地理定位(除非有人知道吗?)

显然 Gecko 浏览器确实支持地理定位,但这对我来说不是一个选项,因为客户端可能安装了不同的 firefox 版本。

最佳答案

功劳归功于 Alex Filipovici 的答案在这个问题上给出:C# desktop application doesn't share my physical location

下面是转换为VB的代码:

网络服务器类:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Text
Imports System.Threading

Public Class WebServer
Private ReadOnly _listener As New HttpListener()
Private Shared _staticContent As String

Public Sub New(ByVal prefixes() As String, ByVal content As String)
_staticContent = content
For Each s As String In prefixes
_listener.Prefixes.Add(s)
Next s
_listener.Start()
End Sub

Public Sub New(ByVal content As String, ByVal ParamArray prefixes() As String)
Me.New(prefixes, content)
End Sub

Public Sub Run()
ThreadPool.QueueUserWorkItem(Sub(o)
Try
Do While _listener.IsListening
ThreadPool.QueueUserWorkItem(Sub(c)
Dim ctx = TryCast(c, HttpListenerContext)
Try
Dim buf() As Byte = Encoding.UTF8.GetBytes(_staticContent)
ctx.Response.ContentLength64 = buf.Length
ctx.Response.OutputStream.Write(buf, 0, buf.Length)
Catch
Finally
ctx.Response.OutputStream.Close()
End Try
End Sub, _listener.GetContext())
Loop
Catch
End Try
End Sub)
End Sub

Public Sub [Stop]()
_listener.Stop()
_listener.Close()
End Sub

End Class

表单代码:

Imports System.Reflection
Imports System.Net
Imports System.Threading
Imports System.Text

Public Class Form1
Dim _ws As WebServer
Dim _webbrowser1 As WebBrowser

Public Sub New()
InitializeComponent()
_webBrowser1 = New WebBrowser()
_webBrowser1.Visible = False
_webBrowser1.ScriptErrorsSuppressed = True
Dim location = System.Reflection.Assembly.GetExecutingAssembly().Location
_webBrowser1.Navigate(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) & "\test1.html")
AddHandler _webBrowser1.DocumentCompleted, AddressOf webBrowser1_DocumentCompleted
End Sub

Async Sub webBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
If _ws Is Nothing Then
Dim html = _webBrowser1.Document.GetElementsByTagName("html")
Dim response = html(0).OuterHtml
_ws = New WebServer(response, "http://localhost:9999/")
_ws.Run()
_webBrowser1.Navigate("http://localhost:9999/")
Else
Dim latitude As String = ""
Dim longitude As String = ""

Await Task.Factory.StartNew(Sub()
While String.IsNullOrEmpty(latitude)
System.Threading.Thread.Sleep(1000)

If Me.InvokeRequired Then
Me.Invoke(DirectCast(Sub()
Dim latitudeEl = _webbrowser1.Document.GetElementById("latitude")
Dim longitudeEl = _webbrowser1.Document.GetElementById("longitude")

latitude = latitudeEl.GetAttribute("value")
longitude = longitudeEl.GetAttribute("value")

End Sub, MethodInvoker))
End If
End While

End Sub)
txtLocation.Text = String.Format("{0},{1}", latitude, longitude)
End If
End Sub
End Class

test1.html文件

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<script type="text/javascript">
window.onload = function () {
var latitude = document.getElementById("latitude");
var longitude = document.getElementById("longitude");

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
else { }
}
function showPosition(position) {
latitude.value = position.coords.latitude;
longitude.value = position.coords.longitude;
}
getLocation();
}
</script>
</head>
<body>
<input type="hidden" id="latitude" />
<input type="hidden" id="longitude" />
</body>
</html>

关于javascript - WinForms 应用程序中的地理定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16716881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com