Saturday, 20 July 2013

Show Mulitiple Markers on Google Map

Here is an example through which u can learn how to show more than one marker on google map.
First of all take a div to show the map on your aspx page like this :-

 <div id="map" style="width: 500px; height: 400px;"></div>

After that you have to add a google api which will display the google map on  your div portion:-

<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript">

Now add this script on client side:- In this i am adding six locations on variable locations and setting other properties of map like zoom level,center,maptype.
Also i am showing infowindow on click of marker for which i took variable infowindow.
Set the below script on your page and you will be able to show multiple markers:--




<script type="text/javascript">
    var locations = [
      ['Chawri Bazaar', 28.649319,77.224417],
      ['Shivaji Bridge', 28.636212,77.226648],
      ['Rajeev chock Metro', 28.633199,77.21858],
      ['Jhandewalan', 28.644498,77.207594],
      ['Chandni Chock Metro',28.657755,77.228022],
      ['Delhi Junction Railway',28.661972,77.237806]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 13,
      center: new google.maps.LatLng(28.649319,77.224417),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) { 
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
  </script>




Hope this example will help you to complete your purpose. :)

No comments:

Post a Comment