0

I am using the code below to show the marker for each location saved in MySQL database.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>All Locations</title>
        <link rel="stylesheet" href="css/alllocationsstyle.css" type="text/css" media="all" />
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script>
        <script type="text/javascript"> 
            var customIcons = {
            0: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
            shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            },
            1: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png',
            shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            }
            };

            function load() { 
            var map = new google.maps.Map(document.getElementById("map"), { 
            center: new google.maps.LatLng(54.312195845815246,-4.45948481875007), 
            zoom:6, 
            mapTypeId: 'roadmap' 
            }); 

            var infoWindow = new google.maps.InfoWindow;

            // Change this depending on the name of your PHP file 
            downloadUrl("phpfile.php", function(data) { 
            var xml = data.responseXML; 
            var markers = xml.documentElement.getElementsByTagName("marker"); 
            for (var i = 0; i < markers.length; i++) { 
            var locationname = markers[i].getAttribute("locationname"); 
            var address = markers[i].getAttribute("address");
            var finds = markers[i].getAttribute("finds");
            var totalfinds = markers[i].getAttribute("totalfinds");
            var point = new google.maps.LatLng( 
            parseFloat(markers[i].getAttribute("osgb36lat")), 
            parseFloat(markers[i].getAttribute("osgb36lon")));
            var html = locationname + ', No.of finds: ' + "<b>" + totalfinds + "</b>";
            var icon = customIcons[totalfinds] || {};
            var marker = new google.maps.Marker({          
            map: map, 
            position: point,
            title: address,
            icon: icon.icon,
            shadow: icon.shadow
            }); 
            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> 
            </head> 

            <body onLoad="load()">
                <div id="map"></div>
            </body> 
            </html>

I'd now like to be able to extend the functionality of the page a little further by adding a sidebar, whereby if the user clicks on the location in the sidebar it highlights the selection with a yellow background and then pans the map to the center it on the associated marker where it opens up the info window. If the user should click on the marker, I would like the info window to open and the associated location on the sidebar to be highlighted, again, with a yellow background.

I have been researching this for sometime and I found the exact type of sidebar that I would like here http://econym.org.uk/gmap/example_map2c.html but the problem is, is that this is in v2 of the Google maps API.

I just wondered whether it would be at all possible that someone could please show me what I need to do to get this to work.

UPDATED CODE

<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=yes" /> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>All locations</title> 

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 

</head> 
<div id="map_canvas" style="width: 900px;height: 600px;">
</div>  <div id="locations"></div> 

 <body onload="showLocations()"> 

<script> 
  var map; 
var markers = new Array(); 


function showLocations() { 
 downloadUrl("phpfile.php", function(data) { 
    var xml = data.responseXML; 
     markers = xml.documentElement.getElementsByTagName("marker"); 

    for (var i = 0; i < markers.length; i++) { 
      var locationname = markers[i].getAttribute("locationname"); 

      markerId = "id_"+i; 

      location_list += "<div id="+markerId+">"+locationname+"</div>"; 
    }        

    document.getElementById("location_list").innerHTML = location_list; 

    initialize_member_map("lang") 
  }); 
} 

function initialize_member_map(lang) { 
  map = new google.maps.Map(document.getElementById("map-canvas"), { 
    center: new google.maps.LatLng(54.312195845815246,-4.45948481875007), 
    zoom: 6, 
    mapTypeId: 'roadmap' 
  });  

    var xml = data.responseXML; 

    var bounds = new google.maps.LatLngBounds(); 
    for (var i = 0; i < markers.length; i++) { 
      var infoWindow = new google.maps.InfoWindow; 
      var locationname = markers[i].getAttribute("locationname"); 
      var address = markers[i].getAttribute("address"); 
      var locationid = markers[i].getAttribute("locationid"); 
      var point = new google.maps.LatLng( 
            parseFloat(markers[i].getAttribute("osgb36lat")), 
            parseFloat(markers[i].getAttribute("osgb36lon")));
      var html = "<b>" + locationname + "</b> <br/>" + address; 
      bounds.extend(point);  
      var marker = new google.maps.Marker({ 
        map: map, 
        position: point, 
        locationid: locationid 
      }); 

      markerId = "id_"+i; 
      bindInfoWindow(gmarker, map, infoWindow, html, markerId);  

    }        
    map.setCenter(bounds.getCenter()); 
    map.fitBounds(bounds);  


} 

function scrollToMarker(index) { 
    map.panTo(gmarkers[index].getPosition()); 
} 

function bindInfoWindow(marker, map, infoWindow, html, markerId) { 
  google.maps.event.addListener(marker, 'click', function() { 
    infoWindow.setContent(html); 
    infoWindow.open(map, marker); 
  }); 
  markerObj =  document.getElementById(markerId); 
  google.maps.event.addDomListener(markerObj, 'click', function() { 
    infoWindow.setContent(html); 
    infoWindow.open(map, marker); 
    map.panTo(marker.getPosition()); 
  }); 

} 

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>  
</body>
</html>
IRHM
  • 1,326
  • 11
  • 77
  • 130

1 Answers1

1

My recommendation it's looking first documentation there you can find examples and a tutorial, then go to google playgorund which have live examples. It's completely possible make the sidebar, the only thing that you need to consider it's if the sidebar will be inside or outside of the map, in the link of the playground i are the example to make you're own controls. To store the data you need to understand how are make asynchronous calls by ajax to save the locations

Jorge
  • 17,896
  • 19
  • 80
  • 126
  • Hi, many thanks for taking the time to reply to my post. I have already looked at the documentation on the Google site plus many of the examples available through searching on the internet, and although comprehensive from a beginners perspective I found then to be a little difficult to understand which is why I came here. Kind regards. Chris – IRHM Aug 16 '11 at 14:08
  • Hi, I wonder whether someone may be able to help me please. I've found what I hope will be the solution to my problem on this forum at http://stackoverflow.com/questions/4904549/open-infowindow-of-specific-marker-from-outside-google-maps-v3. I've amended this to fit my data, shown as an update to my original post. However when I run this I receive the following error 'object expected at line 33 character 7. I checked this against the original and I can't find out what the problem is. Could someone perhaps please have a look at this to see where I'm going wrong. Many thanks and regards Chris – IRHM Aug 16 '11 at 18:11