【问题标题】:How to add JavaScript function to c#?如何将 JavaScript 函数添加到 c#?
【发布时间】:2016-05-26 15:41:06
【问题描述】:

我正在用 C# 构建一个包含谷歌地图的程序。我正在尝试添加一个根据用户的插入将信息窗口添加到地图的功能。问题是我只找到了一个 JavaScript 代码函数(不完全适合),但我不知道如何将它添加到 c#。 我需要的是一种连接JS函数的方法,或者一个适合的新c#函数。 这是我找到的 JS 函数:

// This example displays a marker at the center of Australia.
// When the user clicks the marker, an info window opens.

function initMap() {
    var uluru = {lat: -25.363, lng: 131.044};
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 4,
        center: uluru
    });

    var contentString = '<div id="content">'+
        '<div id="siteNotice">'+
        '</div>'+
        '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
        '<div id="bodyContent">'+
        '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
        'sandstone rock formation in the southern part of the '+
        'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+
        'south west of the nearest large town, Alice Springs; 450&#160;km '+
        '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+
        'features of the Uluru - Kata Tjuta National Park. Uluru is '+
        'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
        'Aboriginal people of the area. It has many springs, waterholes, '+
        'rock caves and ancient paintings. Uluru is listed as a World '+
        'Heritage Site.</p>'+
        '<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
        'https://en.wikipedia.org/w/index.php?title=Uluru</a> '+
        '(last visited June 22, 2009).</p>'+
        '</div>'+
        '</div>';

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

    var marker = new google.maps.Marker({
        position: uluru,
        map: map,
        title: 'Uluru (Ayers Rock)'
    });
    marker.addListener('click', function() {
        infowindow.open(map, marker);
    });
}

结果如下:https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple

【问题讨论】:

  • 您可能不应该将它添加到 C# 代码中。 C# 是服务器,而 javascript 是(在您的情况下)客户端。您需要将其放在单独的 .js 文件中,并将其包含在您的网页中。
  • 你在使用 C#.NET Webforms 吗?

标签: javascript c#


【解决方案1】:

好吧,我对 Javascript 了解不多,但如果您使用 C#.NET,我知道一种可以帮助您的方法:

public void sayHello()
{
  Response.Write("<script>alert('Hello World!')</script>");
}

或者如果你想在C#制作谷歌地图的表格,你可以这样做

private void BuildScript(DataTable tbl)
{
String Locations = "";
foreach (DataRow r in tbl.Rows)
 {
   // bypass empty rows 
    if (r["Latitude"].ToString().Trim().Length == 0)
        continue;

      string Latitude = r["Latitude"].ToString();
      string Longitude = r["Longitude"].ToString();

   // create a line of JavaScript for marker on map for this record 
    Locations += Environment.NewLine + " map.addOverlay
     (new GMarker(new GLatLng(" + Latitude + "," + Longitude + ")));";
}

// construct the final script
js.Text = @"<script type='text/javascript'>
               function initialize() {
              if (GBrowserIsCompatible()) {
                 var map = new GMap2(document.getElementById('map_canvas'));
                map.setCenter(new GLatLng(51.5,-0.1167), 2); 
               " + Locations + @"
              map.setUIToDefault();
               }
           }
     </script> ";
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2020-04-29
    • 2018-09-02
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多