简体   繁体   中英

I am working with google map multi markers in asp.net

I am working with google map multi markers in asp.net, here I need to send a array value from server side to client side variable. I am using " ClientScript.RegisterArrayDeclaration" for passing array to my client side variable. But it doesn't work. Kindly help me. I need to pass value on page load. " locations" is my java script variable *

<html>
<head>

  <title>Google Maps Multiple Markers</title>
  <script src="http://maps.google.com/maps/api/js?key=My KEy" type="text/javascript"></script>
</head>
<body>
<center>
  <div  id="map" style="height: 500px; width: 700px;">
</div>
</center>
<script type="text/javascript">
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 8,
        center: new google.maps.LatLng(9.918648010, 78.12000),
        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>
</body>
</html>

*

  protected void Page_Load(object sender, EventArgs e)
    {
       List<String> oGeocodeList = new List<String>
      {
     " ['Bala', 9.91864810526, 78.1203081843780, 4] ",
     " ['ganesh', 9.91864, 78.12, 5] ",
     " ['santhosh', 9.9186, 78.120, 3] ",
     " ['test', 9.9186481, 78.12030, 2] ",
     " ['test2', 9.918670, 78.12840, 1] "
     };
        var geocodevalues = string.Join(",", oGeocodeList.ToArray());
        ClientScript.RegisterArrayDeclaration("locations", geocodevalues);

    }

In your example the map is initialized before the locations variable is getting defined on the page. One option would to initialize the map once the page is loaded:

function initMap() {

    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 8,
        center: new google.maps.LatLng(9.918648010, 78.12000),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var infowindow = new google.maps.InfoWindow();
    for (var i = 0; i < locations.length; i++) {
        var 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));
    }

}
google.maps.event.addDomListener(window, 'load', initMap);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM