1

I'm trying to send a IEnumerable model that includes the latitude and longitude values that I want to display on Google map that I display on my web site.

Here is the code that I am using in my view to display the map:

@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>
}

I am currently pre-populating it with dummy data, but I am unable to retrieve the data from my model.

How will I be able to use the model, as I am unable to run a foreach loop in the script?

Thanks in advance!

Found the Answer! I needed to return my JSon data from my controller using this method:

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);
}

thanks for all the help! The Json.Encode was also needed!

5
  • 1
    you want to do multi maps with multi data model? Commented Oct 10, 2014 at 21:39
  • No, I want to display multiple markers on one single map? :) Markers that I store in my model Commented Oct 10, 2014 at 21:57
  • Is it fine like that then? :) Commented Oct 11, 2014 at 14:25
  • Yes and alwayes do that :) Commented Oct 11, 2014 at 15:35
  • @Jets take a look at github.com/jmelosegui/GooglemapMvc Commented Feb 3, 2015 at 16:24

1 Answer 1

2

on your view Json.Encode can convert your list to JSON array

RAZOR

@Html.Raw(Json.Encode(YourModel))

ASPX

<%=html.Raw(Json.Encode(YourModel)%>

-

var data = <%=html.Raw(Json.Encode(YourModel)%>;
Sign up to request clarification or add additional context in comments.

2 Comments

if you are using razor view try var data = @Html.Raw(Json.Encode(YourModel);
Ah yea i see, I understand how to use the @Html.Raw(Json.Encode(YourModel)) now, but I am not sure about the formatting, because the map is not displaying the markers at all?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.