Javascript MVC asp.net中谷歌地图Api上的多个标记

Javascript MVC asp.net中谷歌地图Api上的多个标记,javascript,c#,google-maps-api-3,Javascript,C#,Google Maps Api 3,我正在使用Asp.NETMVC开发一个web应用程序,我需要在GoogleMapsAPI JavaScript上添加多个标记 在视图中,我调用了一个模型,它是一个车辆列表对象 <script> function initMap() { var myLatLng = { lat: 48.584715, lng: 7.748556 }; // Create a map object and specify the DOM element // for d

我正在使用Asp.NETMVC开发一个web应用程序,我需要在GoogleMapsAPI JavaScript上添加多个标记

在视图中,我调用了一个模型,它是一个车辆列表对象

<script>



function initMap() {

    var myLatLng = { lat: 48.584715, lng: 7.748556 };

    // Create a map object and specify the DOM element
    // for display.
    var map = new google.maps.Map(document.getElementById('carte'), {
        center: myLatLng,
        zoom: 14
    });

    // Create a marker and set its position.

    @foreach(var item in Model){
         <text>
              var marker = new google.maps.Marker({
              map: map,
              position: { lat: @item.VehicleLocation.Latitude, lng: @item.VehicleLocation.Longitude },
              title: 'Hello World!'
        </text>
        }

}</script>

函数initMap(){
var Mylatng={lat:48.584715,lng:7.748556};
//创建映射对象并指定DOM元素
//用于显示。
var map=new google.maps.map(document.getElementById('carte'){
中心:myLatLng,
缩放:14
});
//创建标记并设置其位置。
@foreach(模型中的var项目){
var marker=new google.maps.marker({
地图:地图,
位置:{lat:@item.VehicleLocation.Latitude,lng:@item.VehicleLocation.Longitude},
标题:“你好,世界!”
}
}
我使用
@foreach
块为列表中的每个对象放置一个标记

没有这个@foreach部分,我们可以看到地图。 但是当我放置@foreach时,地图不会显示

我不明白这个问题


如何解决此问题?

由于未关闭google.maps.Marker(…)调用,您正在抛出语法错误

@foreach(模型中的变量项){
var marker=new google.maps.marker({
地图:地图,
位置:{lat:@item.VehicleLocation.Latitude,lng:@item.VehicleLocation.Longitude},
标题:“你好,世界!”
});
}

请注意关闭按钮(代码>)

由于未关闭google.maps.Marker(…)调用,因此引发语法错误

@foreach(模型中的变量项){
var marker=new google.maps.marker({
地图:地图,
位置:{lat:@item.VehicleLocation.Latitude,lng:@item.VehicleLocation.Longitude},
标题:“你好,世界!”
});
}
请注意关闭按钮(代码>)

好的

所以问题出在我的对象定义上。 纬度和经度为双值,而不是字符串值。 所以坐标是逗号,而不是点

我在对象中添加了两个字符串字段,并将坐标转换为:

在控制器中:

                foreach(var item in VehiculesList)
        {

            item.VehicleLocation.Lat_String = item.VehicleLocation.Latitude.ToString().Replace(",", ".");
            item.VehicleLocation.Long_String = item.VehicleLocation.Longitude.ToString().Replace(",", ".");
        }
他认为:

<script>



function initMap() {

    var myLatLng = { lat: 48.584715, lng: 7.748556 };

    // Create a map object and specify the DOM element
    // for display.
    var map = new google.maps.Map(document.getElementById('carte'), {
        center: myLatLng,
        zoom: 14
    });

    // Create a marker and set its position.

    @foreach(var item in Model){
         <text>
              var marker = new google.maps.Marker({
              map: map,
              position: { lat: @item.VehicleLocation.Lat_String, lng: @item.VehicleLocation.Long_String },
              title: 'Hello World!'
            });
        </text>

    }

}</script>

函数initMap(){
var Mylatng={lat:48.584715,lng:7.748556};
//创建映射对象并指定DOM元素
//用于显示。
var map=new google.maps.map(document.getElementById('carte'){
中心:myLatLng,
缩放:14
});
//创建标记并设置其位置。
@foreach(模型中的var项目){
var marker=new google.maps.marker({
地图:地图,
位置:{lat:@item.VehicleLocation.lat_String,lng:@item.VehicleLocation.Long_String},
标题:“你好,世界!”
});
}
}
好的

所以问题出在我的对象定义上。 纬度和经度为双值,而不是字符串值。 所以坐标是逗号,而不是点

我在对象中添加了两个字符串字段,并将坐标转换为:

在控制器中:

                foreach(var item in VehiculesList)
        {

            item.VehicleLocation.Lat_String = item.VehicleLocation.Latitude.ToString().Replace(",", ".");
            item.VehicleLocation.Long_String = item.VehicleLocation.Longitude.ToString().Replace(",", ".");
        }
他认为:

<script>



function initMap() {

    var myLatLng = { lat: 48.584715, lng: 7.748556 };

    // Create a map object and specify the DOM element
    // for display.
    var map = new google.maps.Map(document.getElementById('carte'), {
        center: myLatLng,
        zoom: 14
    });

    // Create a marker and set its position.

    @foreach(var item in Model){
         <text>
              var marker = new google.maps.Marker({
              map: map,
              position: { lat: @item.VehicleLocation.Lat_String, lng: @item.VehicleLocation.Long_String },
              title: 'Hello World!'
            });
        </text>

    }

}</script>

函数initMap(){
var Mylatng={lat:48.584715,lng:7.748556};
//创建映射对象并指定DOM元素
//用于显示。
var map=new google.maps.map(document.getElementById('carte'){
中心:myLatLng,
缩放:14
});
//创建标记并设置其位置。
@foreach(模型中的var项目){
var marker=new google.maps.marker({
地图:地图,
位置:{lat:@item.VehicleLocation.lat_String,lng:@item.VehicleLocation.Long_String},
标题:“你好,世界!”
});
}
}

是的,我只是纠正了一下,谢谢,但地图仍然没有显示:/我检查了Firefox中的F12工具,脚本显示了所有google.maps.Marker(),但我刚刚看到了coord。“从我的车辆”对象包含逗号而不是点:like位置:{lat:48567803,lng:7709108},@achencraft ah;好的,我修改了我的对象定义,因为坐标是double,而不是string。所以地图在这里,但所有标记都在同一点上:oEDIT:the map in gone:'(是的,我只是更正了它,谢谢,但地图仍然没有显示:/我检查了Firefox中的F12工具,脚本显示了所有google.maps.Marker(),但我刚刚看到coord.from my vehicles对象包含逗号,而不是点:类似位置:{lat:48567803,lng:7709108},@achencraft-ah;这样就可以了。好的,我修改了我的对象定义,因为坐标是double,而不是string。所以地图在这里,但所有标记都在同一点上:oEDIT:the map in Goe:'(