4
votes

I have a table containing multiple addresses, including their Lat/Long coordinates, and I want to place many of these markers onto a google map at once, using asp.net webforms and Google Maps Javascript API V3.

The tutorials show how to add one marker:
http://code.google.com/apis/maps/documentation/javascript/overlays.html#Markers

 var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var myOptions = {
    zoom: 4,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  var marker = new google.maps.Marker({
      position: myLatlng, 
      map: map, 
      title:"Hello World!"
  });

My question is, after I've loaded the set of multiple addresses server side (codebehind), what is a good way to output this set into the html such that client side javascript can iterate the collection and place markers onto the map.

Update

If I already had created my set, what would be a decent way of pushing that out into the page's html at render time, without calling an external script? (Passing the complex filter parameters to the script would be complicated so I'd rather avoid this.) Should I literally just use a stringbuilder to construct a javascript function containing the proper json array and then append that function to the page? That would work, but it doesn't seem quite proper.

5
How many markers are you working with? The order of magnitude matters a lot.Matt Ball
Oh nothing huge, let's just say 50 markers.tbone

5 Answers

6
votes

You could go with the approach you are suggesting.

This is a very simple example showing how easy it is to plot multiple markers with the v3 API:

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>

  <script type="text/javascript">

    var map;

    // Cretes the map
    function initialize() {
      map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: new google.maps.LatLng(-33.92, 151.25),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
    }

    // This function takes an array argument containing a list of marker data
    function generateMarkers(locations) {
      for (var i = 0; i < locations.length; i++) {  
        new google.maps.Marker({
          position: new google.maps.LatLng(locations[i][1], locations[i][2]),
          map: map,
          title: locations[i][0]
        });
      }
    }
  </script>

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

Then to generate the markers you can dump the following script anywhere within your <body> tags.

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

  <script type="text/javascript">
    window.onload = function () {
      initialize();
      generateMarkers(
        ['Bondi Beach', -33.890542, 151.274856],
        ['Coogee Beach', -33.923036, 151.259052],
        ['Cronulla Beach', -34.028249, 151.157507],
        ['Manly Beach', -33.800101, 151.287478],
        ['Maroubra Beach', -33.950198, 151.259302]
      );
    };
  </script>
</body>

You would simply need to generate the array literal ['Bondi Beach', -33.890542, 151.274856] ... from your server-side data set, since the rest is static. Make sure that the last element does not end with a comma.

1
votes

Here's what I was able to get from the Google Maps API v3 Group a while back, for working with tens of thousands of points - specifically, check out what Paul Kulchenko threw together. It's pretty awesome.

http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/59a52aecec860e75?pli=1

0
votes

I think that the obvious approach will work fine. After you initialize the map you can make an asynchronous request to a script that produces a JSON array of lat long coordinates. Iterate over that list placing a marker at each coordinate, or hand off each marker to the marker manager as suggested by Steve.

If you have too many markers to make this approach feasible, then you should load the markers incrementally. Your task will then be to decide when to trigger the asynchronous request which will add the markers for a given subset of the map.

0
votes

I think generateMarkers function should call like this. because if i call locations[i][1] in generateMarkers function it will only gives me first character of passed argument. so, i had to convert my normal array to json. It worked for me.

generateMarkers([
 {'0':'Bondi Beach', '1':-33.890542, '2':151.274856},
 {'0':'Coogee Beach', '1':-33.923036, '2':151.259052},
 {'0':'Cronulla Beach', '1':-34.028249, '2':151.157507},
 {'0':'Manly Beach', '1':-33.800101, '2':151.287478},
 {'0':'Maroubra Beach', '1':-33.950198, '2':151.259302}
]);
0
votes

Update to get markers:

<script type="text/javascript">
    var arr = [
            ['Bondi Beach', -33.890542, 151.274856],
            ['Coogee Beach', -33.923036, 151.259052],
            ['Cronulla Beach', -34.028249, 151.157507],
            ['Manly Beach', -33.800101, 151.287478],
            ['Maroubra Beach', -33.950198, 151.259302]
    ]
        window.onload = function () {
          initialize();
          generateMarkers(arr);
        };

      </script>