cmccarra
cmccarra

Reputation: 199

Google Maps API - External link to map markers and open infowindow

I have a map populating with markers taken from an xml file that are stored in a database. Below the map I am pulling the same info from each marker and displaying them as listings. What I'm trying to to is create a <a href=""> type link that will open the corresponding marker and infowindow beside each listing. Here is my page, it might better explain what I mean: http://poultry.ie/plugin/page/breeders

Here is my code:

 <script type="text/javascript">
    //<![CDATA[

var redpin = new google.maps.MarkerImage('http://labs.google.com/ridefinder/images/mm_20_red.png',
    new google.maps.Size(20,32),
    new google.maps.Point(0,0),
    new google.maps.Point(0,32)
);
var redpinshadow = new google.maps.MarkerImage('http://labs.google.com/ridefinder/images/mm_20_shadow.png',
    new google.maps.Size(37,32),
    new google.maps.Point(0,0),
    new google.maps.Point(0, 32)
);


   function load() {
       var gmarkers = [];
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(53.5076512854544, -7.701416015625),
        zoom: 7,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP file
       downloadUrl("http://poultry.ie/plugins/CustomPages/pages/phpsqlajax_genxml3.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
          var phone = markers[i].getAttribute("phone");
          var breeds = markers[i].getAttribute("breeds");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + "</b><br />" + address + "<br/>" + phone + "<br/>" + breeds;
      var marker = new google.maps.Marker({
        map: map,
        shadow: redpinshadow,
        icon: redpin,
        position: point
      });

          bindInfoWindow(marker, map, infoWindow, html);
        }
      });
    }

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function doNothing() {}

    //]]>

    </script>

And the php that is dynamically displaying the listings (for this one county):

//Armagh
 $data = mysql_query("SELECT * FROM markers WHERE address='Armagh'")
 or die(mysql_error());
 while($info = mysql_fetch_array( $data )) 
 { 
 Print '<div class="county all armagh">'; 
 Print "<h4>".$info['name'] . "</h4> "; 
 Print "<em>".$info['address'] . "</em><br /> "; 
 Print "".$info['phone'] . "<br /> "; 
 Print '<a href="javascript:myclick(' . $info['id'] . ')">See on Map</a><br />';
 Print "<em>Breeds:</em> ".$info['breeds'] . "<hr/></div>";

 } 

The <a href="javascript:myclick( is from a previous attempt at creating this, it doesn't actually have a function at the moment.

I have tried to apply many examples to my code without success as my knowledge of javascript is fairly limited to say the least. My above code might also not be the cleanest as it is my putting together from a lot of examples.

Upvotes: 1

Views: 7334

Answers (1)

geocodezip
geocodezip

Reputation: 161324

Here is an example (ported from Mike Williams' v2 tutorial) which loads markers from an xml file and has a clickable sidebar

Upvotes: 1

Related Questions