Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#代码中使用javaScript库_Javascript_C#_Google Maps Markers - Fatal编程技术网

如何在c#代码中使用javaScript库

如何在c#代码中使用javaScript库,javascript,c#,google-maps-markers,Javascript,C#,Google Maps Markers,我正在使用MVC4 我正在尝试访问我的供应商数据,以便在谷歌地图中标记位置 public ActionResult GoogleMaps() { var dbContext = new VicoProject___NewVersion.DAL.MakeUpContext(); ViewBag.supliersContext = dbContext.Supplier.ToArray(); eturn View(); } 以上代码位于myHomeController <

我正在使用
MVC4

我正在尝试访问我的供应商数据,以便在谷歌地图中标记位置

public ActionResult GoogleMaps()
{
    var dbContext = new VicoProject___NewVersion.DAL.MakeUpContext();
    ViewBag.supliersContext = dbContext.Supplier.ToArray();
    eturn View();
}
以上代码位于my
HomeController

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API-key">
</script>
<script type="text/javascript">
    function initialize() {
        var mapOptions = {
            center: { lat: 32.071953, lng: 34.787868 },
            zoom: 8
            };

        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        @Html.Raw(ViewBag.HTTML)

        @foreach (var c in ViewBag.supliersContext)
        {
            var supplierAddress = c.SupplierAddress;
            var suplierName = c.SupplierName;
            var supplierLat = c.SupplierLatitude;
            var supplierLng = c.SupplierLongitude;
            // cant use the google.maps :(:(
            var marker = new google.maps.Marker({
                position: google.maps.LatLng(supplierLat,supplierLng);
                map: map,
                title: suplierName
            });
        }
     }
    window.onload = initialize;
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

但是仍然没有标记:(

JavaScript不是服务器端语言,因此不能在C#上下文中使用它的对象

基于您的代码,您可以使用JavaScript生成字符串,并使用从控制器检索到的值填充该字符串,类似于@foreach循环中的值:

@(String.Format("new google.maps.Marker({{position: new google.maps.LatLng({0},{1}), map: map, title: {2}    }});", supplierLat, supplierLng, suplierName))
基本上,这将在每次迭代中输出
google.maps.Marker
实例,您可以按照自己的意愿进行格式化,具体取决于您将如何使用它

更新:

这是一个很好的例子

服务器端:

public class Supplier
{
    public string SupplierName { get; set; }
    public decimal SupplierLatitude { get; set; }
    public decimal SupplierLongitude { get; set; }
}

[HttpGet]
public ActionResult Index()
{
    ViewBag.supliersContext = new List<Supplier>()
    {
        new Supplier() { SupplierName = "Test1", SupplierLatitude = 32.071953M, SupplierLongitude = 34.787868M },
        new Supplier() { SupplierName = "Test2", SupplierLatitude = 31.571953M, SupplierLongitude = 34.787868M },
        new Supplier() { SupplierName = "Test3", SupplierLatitude = 31.071953M, SupplierLongitude = 34.787868M },
    };
    return View();
}
公共类供应商
{
公共字符串供应商名称{get;set;}
公共十进制供应商纬度{get;set;}
公共十进制供应器经度{get;set;}
}
[HttpGet]
公共行动结果索引()
{
ViewBag.supliersContext=新列表()
{
新供应商(){SupplierName=“Test1”,供应商纬度=32.071953M,供应商经度=34.787868M},
新供应商(){SupplierName=“Test2”,供应商纬度=31.571953M,供应商经度=34.787868M},
新供应商(){SupplierName=“Test3”,供应商纬度=31.071953M,供应商经度=34.787868M},
};
返回视图();
}
客户端:

<html lang="en">        
    <body>
        <div id="map-canvas" style="height: 400px;"></div>
        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
        <script type="text/javascript">
            function initialize() {
                var mapOptions = {
                    center: { lat: 32.071953, lng: 34.787868 },
                    zoom: 8
                };
                var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
                @foreach (var c in ViewBag.supliersContext)
                {
                    var suplierName = c.SupplierName;
                    var supplierLat = c.SupplierLatitude;
                    var supplierLng = c.SupplierLongitude;
                    @Html.Raw(String.Format("new google.maps.Marker({{position: new google.maps.LatLng({0},{1}), map: map, title: '{2}'}});", supplierLat, supplierLng, suplierName))
                }

            }
            window.onload = initialize;
            google.maps.event.addDomListener(window, 'load', initialize);
        </script>
    </body>
</html>

函数初始化(){
变量映射选项={
中心:{lat:32.071953,lng:34.787868},
缩放:8
};
var map=new google.maps.map(document.getElementById('map-canvas'),mapOptions);
@foreach(ViewBag.supliersContext中的变量c)
{
var suplierName=c.suplierName;
var supplierLat=c.供应商纬度;
var supplierLng=c.供应商经度;
@Html.Raw(String.Format(“new google.maps.Marker({{position:new google.maps.LatLng({0},{1}),map:map,title:{2}}}});”,supplierLat,supplierLng,suppliername))
}
}
window.onload=初始化;
google.maps.event.addDomListener(窗口“加载”,初始化);

这只是一个测试示例,您不应该立即使用它,因为@Html.Raw会显示该上下文中的漏洞。

1)最好隐藏您的google maps密钥。2) 您将无法从c#上下文访问JS对象。因此,您需要返回JSON格式的数据,以便稍后将其转换为标记,或者使用JS代码返回@foreach构建字符串,并在脚本标记中输出数据。@Vladimirs Y您没有添加答案吗?顺便说一句,仅仅像这样隐藏它是不够的。。。我仍然可以在编辑历史xD中看到它。若你们能重新生成它,那个么我会这么做。我已经为它升起了一个版主的注意标志,希望有人能把它清除掉history@Coulton,可能会有一些选项/解决方法(node.js/某种代理,但不确定)但我相信情况并非如此。我想我应该将此stringFormat转换为javaScript函数?@IgnacioP到底什么不起作用?我更新了我的评论-
new
google.maps.LatLng({0},{1})
中丢失。不,您不需要执行
@()
应该输出内容,在您的情况下,它将输出“new google.maps.Marker({position:new google.maps.LatLng(1.1,2.2),map:map,title:“Marker1”})new google.maps.Marker({{position:new google.maps.LatLng(1.2,2.3),map:map,title:“Marker2”})…”@IgnacioP再次更新了答案。从在.NET服务器端运行JS开始。我的团队在一个项目中做到了这一点。有几种方法(Jurassic.NET、Edge.js等)。以下是我的帖子:
<html lang="en">        
    <body>
        <div id="map-canvas" style="height: 400px;"></div>
        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
        <script type="text/javascript">
            function initialize() {
                var mapOptions = {
                    center: { lat: 32.071953, lng: 34.787868 },
                    zoom: 8
                };
                var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
                @foreach (var c in ViewBag.supliersContext)
                {
                    var suplierName = c.SupplierName;
                    var supplierLat = c.SupplierLatitude;
                    var supplierLng = c.SupplierLongitude;
                    @Html.Raw(String.Format("new google.maps.Marker({{position: new google.maps.LatLng({0},{1}), map: map, title: '{2}'}});", supplierLat, supplierLng, suplierName))
                }

            }
            window.onload = initialize;
            google.maps.event.addDomListener(window, 'load', initialize);
        </script>
    </body>
</html>