Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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
Javascript 使用mvc模型在Google Maps API v3上显示标记_Javascript_Asp.net Mvc_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 使用mvc模型在Google Maps API v3上显示标记

Javascript 使用mvc模型在Google Maps API v3上显示标记,javascript,asp.net-mvc,google-maps,google-maps-api-3,Javascript,Asp.net Mvc,Google Maps,Google Maps Api 3,我正在尝试发送一个IEnumerable模型,其中包含我希望在我的网站上显示的Google地图上显示的纬度和经度值 以下是我在视图中用于显示地图的代码: @model IEnumerable<ProjectName.Models.modelname> @{ ViewBag.Title = "Map View"; } <script src="http://maps.google.com/maps/api/js?key=My-API-Key" type="text/ja

我正在尝试发送一个IEnumerable模型,其中包含我希望在我的网站上显示的Google地图上显示的纬度和经度值

以下是我在视图中用于显示地图的代码:

@model IEnumerable<ProjectName.Models.modelname>
@{
    ViewBag.Title = "Map View";
}

<script src="http://maps.google.com/maps/api/js?key=My-API-Key" type="text/javascript"></script>


<style>
    #map_canvas img {
    max-width: none;
    }
</style>


<style>
    .infoDiv {
    height: 200px;
    width: 300px;
    -webkit-user-select: none;
    background-color: white;
}
</style>

<h2>Map</h2> <a class="btn btn-default" href="/Tags/Create">Create Tag &raquo;</a>
<hr />

<div id="map_canvas" style="height: 600px;"></div>


@section scripts {
<section class="scripts">

    <script type="text/javascript">


$(document).ready(function () {
    Initialize();
});


function Initialize() {


    google.maps.visualRefresh = true;
    var Liverpool = new google.maps.LatLng(-26.71452, 27.097047);


    var mapOptions = {
        zoom: 14,
        center: Liverpool,
        mapTypeId: google.maps.MapTypeId.G_NORMAL_MAP
    };


    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    var myLatlng = new google.maps.LatLng(-26.674359 , 27.095391);

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: 'Tate Gallery'
    });


    marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png')


    var data = [
              { "Id": 1, "PlaceName": "Liverpool Museum", "OpeningHours":"9-5, M-F","GeoLong": "53.410146", "GeoLat": "-2.979919" },
              { "Id": 2, "PlaceName": "Merseyside Maritime Museum ", "OpeningHours": "9-1,2-5, M-F", "GeoLong": "53.401217", "GeoLat": "-2.993052" },
              { "Id": 3, "PlaceName": "Walker Art Gallery", "OpeningHours": "9-7, M-F", "GeoLong": "53.409839", "GeoLat": "-2.979447" },
              { "Id": 4, "PlaceName": "National Conservation Centre", "OpeningHours": "10-6, M-F", "GeoLong": "53.407511", "GeoLat": "-2.984683" }
           ];


    $.each(data, function (i, item) {
        var marker = new google.maps.Marker({
            'position': new google.maps.LatLng(item.GeoLong, item.GeoLat),
            'map': map,
            'title': item.PlaceName
        });


        marker.setIcon('http://maps.google.com/mapfiles/ms/icons/blue-dot.png')


        var infowindow = new google.maps.InfoWindow({
            content: "<div class='infoDiv'><h2>" + item.PlaceName + "</h2>" + "<div><h4>Opening hours: " + item.OpeningHours + "</h4></div></div>"
        });

        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map, marker);
        });

    })
}


    </script>
</section>
}

谢谢你的帮助!还需要Json.Encode

在您的视图上,Json.Encode可以将您的列表转换为Json数组

剃刀

ASPX


-

var数据=;

是否要使用多数据模型制作多个地图?否,我要在一个地图上显示多个标记?:)我存储在模型中的标记那么这样好吗?:)是的,而且总是这样:)太好了,我会这样做的,再次感谢你!:)如果您使用的是razor视图,请尝试
var data=@Html.Raw(Json.Encode(YourModel);
Ah是的,我知道了,我现在知道如何使用@Html.Raw(Json.Encode(YourModel)),但我不确定格式,因为地图根本没有显示标记?
public ActionResult MapView()
{
    var tags = db.Tags.ToList();

    var jsonTags = from x in tags
                   select new Tags
                   {
                       GeoName = x.GeoName,
                       GeoLat = x.GeoLat,
                       GeoLong = x.GeoLong
                   };

    return View(jsonTags);
}
@Html.Raw(Json.Encode(YourModel))
<%=html.Raw(Json.Encode(YourModel)%>
var data = <%=html.Raw(Json.Encode(YourModel)%>;