views:

85

answers:

4

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.

A: 

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.

+1  A: 

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

Matt Ball
+1  A: 

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.

Daniel Vassallo
Excellent...but, does this run as is, or does: generateMarkers(locations); need to be called at the end of initialize()...and "generateMarkers([" should actually be "locations([", or am I mistaken?
tbone
@tbone: My previous example had a small flaw. It's better if you use `window.onload` as the updated answer. I've tested it and it works. You can put the `window.onload` script block anywhere in your HTML. As for the argument of `generateMarkers()` you can pass an "anonymous" array using the [array literal notation](https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Literals) as in my example.
Daniel Vassallo
Cool, thanks for the update, works great!
tbone