我有一个 WPF 应用程序,它可以找到纬度和经度的位置,并将它们显示给应用程序内的用户。该应用程序有一个按钮,当点击该按钮时,应该会使用 Google map API 打开 map 并使用标记在 map 上显示这些位置。
我在 HTML 中得到了下面的脚本,它现在只是在固定位置打开 map 并仅显示示例标记。
<script>
function initMap() {
let myLatLng = {lat: -25.363, lng: 131.044};
let map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatLng
});
let marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'TITLE!'
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=<myKey>&callback=initMap">
</script>
所以我知道要打开页面本身,我需要做的就是:
System.Diagnostics.Process.Start(pathToHtmlFile);
但是我怎样才能将我在 WPF 端找到的位置传递给 JS initMap
函数呢?
最佳答案
在文件打开命令中使用 GET 语义。
您必须使用 URI 作为命令:"file://pathToHtmlFile?lat="+ Lat + "&lng="+ Long"
在脚本 block 中添加一个函数:
function get(name){
if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
return decodeURIComponent(name[1]);
并调用 lat 和 lng 的 get 函数。
关于javascript - 如何将信息从 WPF 应用程序传递到 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56297596/