3 回答

TA貢獻1789條經(jīng)驗 獲得超8個贊
<text>
<script type="text/javascript"> // Some JavaScript code here to display map, etc. // Now add markers @foreach (var item in Model) { <text> var markerlatLng = new google.maps.LatLng(@(Model.Latitude), @(Model.Longitude)); var title = '@(Model.Title)'; var description = '@(Model.Description)'; var contentString = '<h3>' + title + '</h3>' + '<p>' + description + '</p>' var infowindow = new google.maps.InfoWindow({ content: contentString }); var marker = new google.maps.Marker({ position: latLng, title: title, map: map, draggable: false }); google.maps.event.addListener(marker, 'click', function () { infowindow.open(map, marker); }); </text> }</script>
最新情況:
@:
<text>
<script type="text/javascript"> // Some JavaScript code here to display map, etc. ... // Declare addMarker function function addMarker(latitude, longitude, title, description, map) { var latLng = new google.maps.LatLng(latitude, longitude); var contentString = '<h3>' + title + '</h3>' + '<p>' + description + '</p>'; var infowindow = new google.maps.InfoWindow({ content: contentString }); var marker = new google.maps.Marker({ position: latLng, title: title, map: map, draggable: false }); google.maps.event.addListener(marker, 'click', function () { infowindow.open(map, marker); }); } // Now add markers @foreach (var item in Model) { @:addMarker(@item.Latitude, @item.Longitude, '@item.Title', '@item.Description', map); }</script>
addMarker
@:
addMarker
@item.Property
更新2
.js
data-
視圖代碼
@foreach(var item in Model){ <div data-marker="@Json.Encode(item)"></div>}
JavaScript代碼
$('[data-marker]').each(function() { var markerData = $(this).data('marker'); addMarker(markerData.Latitude, markerData.Longitude, markerData.Description, markerData.Title);});

TA貢獻1752條經(jīng)驗 獲得超4個贊
App_Code/JS.cshtml
:
@using System.Web.Script.Serialization@helper Encode(object obj){ @(new HtmlString(new JavaScriptSerializer().Serialize(obj)));}
var title = @JS.Encode(Model.Title);
添加回答
舉報