【发布时间】: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 km (208 mi) '+
'south west of the nearest large town, Alice Springs; 450 km '+
'(280 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#