Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the project I'm working on right now, this is how we handle it:</p> <p>Let the main ViewModel be called <code>FooViewModel</code>.</p> <p>ViewModels:</p> <pre><code>public class FooViewModel { // Your search results: public IEnumerable&lt;FooWithLocationViewModel&gt; Foos { get; set; } // Properties for your search filter: public decimal? MaxPrice { get; set; } public CityEnum? City { get; set; } ... } public class FooWithLocationViewModel { public string Name { get; set; } public decimal Price { get; set; } ... public double Latidude { get; set; } public double Longitude { get; set; } ... } </code></pre> <p>In the view, there is a <code>&lt;div&gt;</code> for each <code>FooWithLocationViewModel</code>. We will make use of <code>data-*</code> attributes which are valid in HTML5:</p> <p>View:</p> <pre><code>@model FooViewModel ... @foreach(FooWithLocationViewModel foo in Model.Foos) { &lt;div class="foo" data-latitude="@(foo.Latitude)" data-longitude="@(foo.Longitude)"&gt; &lt;span&gt;@foo.Name&lt;/span&gt; &lt;span&gt;@foo.Price&lt;/span&gt; ... &lt;/div&gt; } &lt;div id="map-canvas"&gt;&lt;/div&gt; </code></pre> <p>Now, it is time for JavaScript, I am assuming that you are using JQuery:</p> <p>Initializing the map, adding the markers and storing them in <code>markers</code> array:</p> <pre><code>function initialize() { var foos = $(".foo"); var markers = new Array(); var mapOptions = { ... }}; var mapCanvas = document.getElementById('map-canvas'); if (mapCanvas != null) { map = new google.maps.Map(mapCanvas, mapOptions); $.each(foos, function (key, value) { markers[key] = new google.maps.Marker({ map: map, draggable: false, animation: google.maps.Animation.DROP, position: new google.maps.LatLng( Number($(value).attr("data-latitude")), Number($(value).attr("data-longitude") )); }); google.maps.event.addListener(markers[key], 'click', function () { // If you need this... }); }); } } </code></pre> <p>What's good about this approach is, everything is done in the function that initializes your Google Map. Thus your script does not have to be embedded in your View. We are using an HTML5 friendly approach. And it is simple.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload