提问人:AirFlow 提问时间:9/2/2023 更新时间:9/2/2023 访问量:45
传单地图和 VB.net
Leafet map & VB.net
问:
我在尝试从包含 pos lats 和 long 的 xml 文件加载的地图上绘制航点的路线时遇到了扩展问题。传单地图按预期加载,并用标记绘制航点,但似乎不喜欢用多边形线连接它们。有什么想法吗?
运行代码时,我在调试输出中收到以下错误行:“未捕获的 TypeError:无法读取 null 的属性(读取'0')”,来源:https://unpkg.com/[email protected]/dist/leaflet.js (5)
我尝试了许多不同的方法来重新排序事件以及如何调用函数,但都没有成功。
HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stamen Terrain Map</title>
<!-- Leaflet CSS -->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<!-- Leaflet JavaScript -->
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
</head>
<body>
<div id="map" style="height: 500px;"></div>
<button id="showRouteButton">Show Route</button>
<script>
var map = L.map('map').setView([0, 0], 2); // Centered on the world map, zoom level 2
L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/terrain/{z}/{x}/{y}.jpg', {
maxZoom: 14,
attribution: '© <a href="https://stamen.com/">Stamen Design</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
// Creating an empty feature group to store the route lines
var routeLines = L.featureGroup().addTo(map);
// Function to plot a waypoint on the map
function plotWaypoint(lat, lng) {
L.marker([lat, lng]).addTo(map);
}
// Function to display the route
function showRoute(routeCoordinates) {
if (routeCoordinates.length > 1) {
// Clear any previous route lines
routeLines.clearLayers();
for (var i = 0; i < routeCoordinates.length - 1; i++) {
var startPoint = routeCoordinates[i];
var endPoint = routeCoordinates[i + 1];
// Check if startPoint and endPoint are defined
if (startPoint && endPoint) {
var route = [startPoint, endPoint];
L.polyline(route, { color: 'blue' }).addTo(routeLines);
}
}
}
}
</script>
</body>
</html>
VB.NET 代码:
Dim webView As ChromiumWebBrowser
Private Sub LoadMap()
' Load your HTML file
Dim htmlFilePath As String = Application.StartupPath & "\WMap.html"
MsgBox(htmlFilePath)
ChromiumWebBrowser1.Load(htmlFilePath)
End Sub
Private Sub Guna2Button6_Click(sender As Object, e As EventArgs) Handles Guna2Button6.Click
LoadMap()
End Sub
Private Sub Guna2Button7_Click(sender As Object, e As EventArgs) Handles Guna2Button7.Click
plotwaypointsfromxml()
End Sub
Private Sub plotwaypointsfromxml()
' Load the XML file
Dim xmlFile As String = Application.StartupPath & "\Test.xml"
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(xmlFile)
' Find the navlog element
Dim navlogNode As XmlNode = xmlDoc.SelectSingleNode("/OFP/navlog")
If navlogNode IsNot Nothing Then
Dim routeCoordinates As New List(Of Object)
' Loop through each fix element
For Each fixNode As XmlNode In navlogNode.SelectNodes("fix")
Dim lat As Double
Dim lng As Double
' Extract latitude and longitude from fix element
Double.TryParse(fixNode.SelectSingleNode("pos_lat").InnerText, lat)
Double.TryParse(fixNode.SelectSingleNode("pos_long").InnerText, lng)
' Add the waypoint to the routeCoordinates array
routeCoordinates.Add(New Object() {lat, lng})
Next
' Plot waypoints first
For Each coord As Object In routeCoordinates
Dim lat As Double = CDbl(coord(0))
Dim lng As Double = CDbl(coord(1))
ChromiumWebBrowser1.ExecuteScriptAsync("plotWaypoint", New Object() {lat, lng})
Next
' Call the JavaScript showRoute function in the WebBrowser control
ChromiumWebBrowser1.ExecuteScriptAsync("showRoute", New Object() {routeCoordinates})
End If
End Sub
答: 暂无答案
评论