1

I am working with the Google Maps API. I have the map displaying on my page, I can add markers to it manually, everything is working correctly.

Now I need to build a marker list from mvc and figure out how to pass it back to the map through asp.net MVC. Everything I need for the marker list is contained in the dtDealer_List shown below.

foreach (DataRow row in dtDealer_List.Rows)
      {
          dealer_list dl = new dealer_list();
          dl.address = row["address"].ToString();
          .......
          lstDealer_List.Add(dl);
      }

I'm not sure on how to pass this list back to the javascript function.

function init_map(map_canvas_id, lat, lng, zoomLevel) {
            var myLatLng = new google.maps.LatLng(lat, lng);

            var options = {
                zoom: zoomLevel,
                center: myLatLng,
                mapTypeId: google.maps.MapTypeId.HYBRID
            };

            var map_canvas = document.getElementById(map_canvas_id);

            var map = new google.maps.Map(map_canvas, options);
        }

Do I need to pull out the dtDealer_List into a ajax call or something?

4
  • do you have the dtDealer_List ready on page load? If so you can just use it as the model or send it in the ViewBag. If not, you'll need to use AJAX Commented Oct 7, 2014 at 16:41
  • dtDealer_List is passed through to the page as the model. How can I use that to populate javascript/jQuery function call? @Jonesy Commented Oct 7, 2014 at 16:44
  • @Jonesy I'll play around with it found a couple of posts on passing model to javascript. Thanks for the idea. Commented Oct 7, 2014 at 16:56
  • I added a solution - took me a bit to remember razor/javascript syntax Commented Oct 7, 2014 at 16:59

1 Answer 1

1

add a small script at the top of your page to set up a javascript array from your list:

@model List<string>

<script>
    var addresses = [];
    @foreach (var address in Model)
    {
        <text>addresses.push('@address')</text>
    }
    console.log(addresses);
</script>

then you should be able to access the global addresses array where you need

Sign up to request clarification or add additional context in comments.

1 Comment

I will play around with that and report back in a little bit, thank you.

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.