Dowemo
0 0 0 0


Question:

I'm creating a ASP.NET MVC3 website using a Google Maps API V3 with a lot of markers. Each marker has an InfoWindow with some information about this place.

I wish each marker have a direct link to access my website directly on this marker, like http://www.mywebsite/1589. So the user could access the website with the map centered on marker 1589 and its InfoWindows would be open.

The markers are already on the map and their InfoWindow are already displaying informations, but I have no idea how to create a direct link to the marker... Could anybody help me ?

Thanks in advance


Best Answer:


The key pieces are:

  • parse the query string

    // skip the first character, we are not interested in the "?"
    var query = location.search.substring(1);
    // split the rest at each "&" character to give a list of  "argname=value"  pairs
    var pairs = query.split("&");
    for (var i=0; i<pairs.length; i++) {
      // break each pair at the first "=" to obtain the argname and value
      var pos = pairs[i].indexOf("=");
      var argname = pairs[i].substring(0,pos).toLowerCase();
      var value = pairs[i].substring(pos+1).toLowerCase();
      // process each possible argname  -  use unescape() if theres any chance of spaces
      if (argname == "id") {id = unescape(value);}
      if (argname == "marker") {index = parseFloat(value);}
    }
  • open the infowindow after loading the markers, if a parameter is passed

      // ========= If a parameter was passed, open the info window ==========
      if (id) {
        if (idmarkers[id]) {
          google.maps.event.trigger(idmarkers[id],"click");
        } else {
          alert("id "+id+" does not match any marker");
        }
      }
      if (index > -1) {
        if (index < gmarkers.length) {
          google.maps.event.trigger(gmarkers[index],"click");
        } else {
          alert("marker "+index+" does not exist");
        }
      }
  • you might also want the "link to" functionality in this example, which sets up the query string




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs