用 C# 重写 javascript 的一部分

Rewrite part of javascript with C#

我正在用 C# 编写这个程序,它应该显示 Google 地图。我正在使用 Google 地图 JavaScript API,这是我能找到的最好的地图。使用该程序,您应该能够搜索地点。

代码:

window.onload = function() {

    var latlng = new google.maps.LatLng(52.785804, 6.897585);
    var options = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map"), options);
}
html, body {
    margin: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

#map {
    width: 80%;
    height: 100%;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>
    <title>Google Maps</title>
    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>

</head>

<body>
    <div id="map">
    </div>
</body>

</html>

我能以某种方式使用 C# 编辑 latlng 吗?或者有人知道用 C# 使用 Google 映射 API 的替代方法吗?

如果您没有在此特定页面上使用 MVC 或其他服务器端技术,您唯一的选择是从 AJAX 调用加载 lat/long

$.ajax({
    url: "url/to/your/api/that/returns/lat/long",
    success: function(result) {
        // process your JSON result and set the lat/long
    }
});

API可以使用任何语言(包括c#)在服务器端编写

在桌面应用程序中显示 Google 地图,例如Winforms,您可以使用 WebBrowser 控件和 HTML 页面(本地文件或作为资源嵌入)。

可以从 C# 窗体或 class 调用 JavaScript 函数,也可以从 JavaScript.

调用 C# 函数

Javascript 到 C#

-------C# code--------------------
[System.Runtime.InteropServices.ComVisible(true)]
// execute the following instruction in the form initialisation
WebBrowser1.ObjectForScripting = this ;
// define a public method
public void ShowMessage (string msg) { MessageBox.Show(msg); }

-------HTML and Javascript----------------
<input type="button" value="JavaScript is calling Dotnet"
onclick="window.external.ShowMessage('JavaScript message');" />

C# 到 Javascript

-------C# code--------------------
object [] MyArgs = { "Hello" } ;   WebBrowser1.Document.InvokeScript("MyJsFunction",MyArgs ) ;

-------Javascript----------------
function MyJsFunction(s)  { alert(s) ; }