views:

43

answers:

1

I'm trying to center a google map on a single marker pulled from a database, what I'm doing so far is using this function to get a single marker from the database

function getMarkers(){
        var urlstr="readsingle.php?v=<?php echo $cam ?>";
        var request = GXmlHttp.create();
        request.open('GET', urlstr , true); // request XML from PHP with AJAX call
        request.onreadystatechange = function () {
            if (request.readyState == 4) { // 4 is a completed request
                var xmlDoc = request.responseXML;
                locations = xmlDoc.documentElement.getElementsByTagName("location");
                markers = [];
                if (locations.length){
                    for (var i = 0; i < locations.length; i++) { // cycle thru locations
                        markers[i] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lng")));
                        markers[i].infowindow = "This is "+locations[i].getAttribute("name");
                        markers[i].markerindex = i;
                        markers[i].db_id = locations[i].getAttribute("location_id");
                        map.addOverlay(markers[i]);

                    }
                }
            }
        }

        request.send(null);

    }

and this function to load the map

    function onLoad() {
      map = new GMap(document.getElementById("div_map"));
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(53.76221, -2.71227), 8);
      map.enableScrollWheelZoom();
      getMarkers();
      GEvent.addListener(map, "click", function(overlay, point) {
          if (overlay){ // marker clicked
              overlay.openInfoWindowHtml(overlay.infowindow);   // open InfoWindow
          } else if (point) {   // background clicked

          }
      });
    }

How can I change the Lat Lng values on this line

map.setCenter(new GLatLng(53.76, -2.71), 5); 

to match the lat lng values from the single marker? or is there a simpler way to pull a single marker from a database and center the map on it?

+1  A: 

If getMarkers() is retrieving just one marker, it looks like you can simply add:

map.setCenter(new GLatLng(locations[i].getAttribute("lat"), 
                          locations[i].getAttribute("lng")), 5);

After or before you add the marker to the map:

map.addOverlay(markers[i]);
Daniel Vassallo
Cheers, didn't realise I could set it within the getmarkers function
FozzDog