title: Adding multiple markers to Google Maps
body: <p>I'm trying to add multiple markers to Google Maps. I am using .NET MVC 4 and I am passing a ViewModel to the View with an array containing all the coordinates. I want to display all these into Google Maps. The array is named latLngCoordinates in the ViewModel. </p>
  
  <pre><code>@model Project.Web.ViewModels.ParkingViewModel
  </code></pre>
  
  <p>Code that goes wrong:</p>
  
  <pre><code>for (var i = 0; i &lt; @Model.latLngCoordinates.Length; i++) {
              addMarker(@Model.latLngCoordinates[i]);
  }
  </code></pre>
  
  <p>Error: The name 'i' does not exist in the current context.</p>
  
  <p>This code is working fine:</p>
  
  <pre><code>&lt;script type="text/javascript"
    src="http://maps.googleapis.com/maps/api/js?sensor=false"&gt;
  &lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      function initialize() {
          var mapOptions = {
              center: new google.maps.LatLng(51.92479, 4.469833),
              zoom: 14,
              mapTypeId: google.maps.MapTypeId.ROADMAP
          };
          var map = new google.maps.Map(document.getElementById("map"),
          mapOptions);
  
          for (var i = 0; i &lt; @Model.latLngCoordinates.Length; i++) {
              addMarker(51.918769, 4.480616);
          }
  
          function addMarker(x, y) {
              var location = new google.maps.LatLng(x, y);
              var marker = new google.maps.Marker({
                  position: location,
                  map: map,
              });
          }
  
      } google.maps.event.addDomListener(window, 'load', initialize);
  &lt;/script&gt;
  </code></pre>
  
  <p>If I use:</p>
  
  <pre><code>addMarker(@Model.latLngCoordinates[1])
  </code></pre>
  
  <p>Or any other value up to 97 it works..</p>