0

I'm trying to put markers on my google map canvas by fetching the info out of the database, I followed every step from https://developers.google.com/maps/articles/phpsqlajax_v3 though somehow it doesnt work (probably because I changed a few things, though I cant find what brings the troubles up.)

I already managed to fetch the data by using a phptoxml.php file the dummy output proves that It's correct I guess:

<users>
<user id="1" name="John Tucker" age="19" lat="47.608940" lng="-122.340141" type="0"/>
<user id="2" name="Dean Jester" age="21" lat="51.219040" lng="4.326590" type="0"/>
<user id="3" name="Joris Nisteven" age="26" lat="51.203671" lng="4.341480" type="0"/>
<user id="4" name="Joske Vermeulen" age="20" lat="51.204155" lng="4.327018" type="0"/>
<user id="5" name="Timmy den Beir" age="21" lat="51.209263" lng="4.339720" type="0"/>
<user id="6" name="Ben von Duppen" age="23" lat="51.168308" lng="4.394287" type="0"/>
</users>

now the code that I use to fetch the data and put it in variables:

downloadUrl("phptoxml.php", function(data) {
        var xml = data.responseXML;
        var users = xml.documentElement.getElementsByTagName("user");
        for (var i = 0; i < users.length; i++) {
            var id = parseInt(users[i].getAttribute("id"));
            var name = users[i].getAttribute("name");
            var age = parseInt(users[i].getAttribute("age"));
            var lat = parseFloat(users[i].getAttribute("lat"));
            var lng = parseFloat(users[i].getAttribute("lng"));
            var marker = add_marker(lat,lng,id,name,age); // pass in as Latitude, then Longitude
            fluster.addMarker(marker);
        }
    });

where fluster is used for cluster management and the function add_marker worked well before, so I doubt that that could be the problem.

the function downloadUrl is defined as followed (though this part of the code is pretty vague for me)

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);
    }

I noticed that if I put an alert behind request.send(null); with debug info, it doesnt pop up. same for any alert that comes after the downloadUrl call..

I have no clue, hope someone might help :)

Ty in advance

for more info i include the entire code:

 <script type="text/javascript">



var map; var fluster; var infoBubble;
  function initialize() {
    var latlng = new google.maps.LatLng(27.059125784374068,-37.6171875);
    var myOptions = {
    zoom: 3,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false,
    streetViewControl: false
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
fluster = new Fluster2(map);


// Change this depending on the name of your PHP file
downloadUrl("phptoxml.php", function(data) {
    var xml = data.responseXML;
    var users = xml.documentElement.getElementsByTagName("user");
    for (var i = 0; i < users.length; i++) {
        var id = parseInt(users[i].getAttribute("id"));
        var name = users[i].getAttribute("name");
        var age = parseInt(users[i].getAttribute("age"));
        var lat = parseFloat(users[i].getAttribute("lat"));
        var lng = parseFloat(users[i].getAttribute("lng"));
        var marker = add_marker(lat,lng,id,name,age); // pass in as Latitude, then Longitude
        fluster.addMarker(marker);
    }
});

var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
    var place = autocomplete.getPlace();
    if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
    } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);  // Why 17? Because it looks good.
    }

    var address = '';
    if (place.address_components) {
        address = [(place.address_components[0] &&
                    place.address_components[0].short_name || ''),
                   (place.address_components[1] &&
                    place.address_components[1].short_name || ''),
                   (place.address_components[2] &&
                    place.address_components[2].short_name || '')
                  ].join(' ');
    }
});

// Initialize Fluster
// This will set event handlers on the map and calculate clusters the first time.
fluster.initialize();   



 }




 function add_marker(lat,lng,id,name,age) {

var marker = new google.maps.Marker({
      position: new google.maps.LatLng(lat,lng),
      map: map,
      icon: 'suiticon.png',
      title: name
});
    var infoBubble = new InfoBubble({
      maxWidth: 300,
      backgroundColor: '#dedddd',
      borderWidth: 2,
      borderColor: 'rgb(68, 68, 68)'
    });

    var contentString = '<div id="content">'+
        /*'<h2>'+name+'</h2>'+
        '<p><span class="myLabel" style="margin-right:10px">'+age+'</span>21</p>'+
        '<p><center><img src="'+id+'.jpg" class="bro_image"></center> </p>'+
        '<p><button class="button_inverse" style="padding-left: 23px; padding-right:23px; margin-left:31px;" href="#">Rate this Bro!</button></p>'+
        */'</div>';

    var div = document.createElement('DIV');
    div.innerHTML = 'No pictures uploaded by this user.';

    infoBubble.addTab('Personal', contentString);
    infoBubble.addTab('Pictures', div);

    google.maps.event.addListener(marker, 'click', function() {
      if (!infoBubble.isOpen()) {
        infoBubble.open(map, marker);
      }
    }); 

return marker;


}


function addTab() {
    var title = document.getElementById('tab-title').value;
    var content = document.getElementById('tab-content').value;

    if (title != '' && content != '') {
      infoBubble.addTab(title, content);
    }
}

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);

}
</script>
10
  • Do you render the map before or after you add the markers? because when i worked with googlemaps i had to first add markers, and then refresh or render the map... Commented Jul 22, 2012 at 15:28
  • I render the map at the very end as far as I'm aware Commented Jul 22, 2012 at 15:33
  • Try to see if you could refresh it or such. I went through hell to get it to work - that was like 4 years ago i think. Commented Jul 22, 2012 at 15:35
  • I added the entire code so you might get a better view on it :) Commented Jul 22, 2012 at 15:38
  • (aside) I'm not sure what the end goal is, but it might be worth double-checking that it won't run up against 10.1.3.b of the Maps TOS (developers.google.com/maps/terms) Commented Jul 22, 2012 at 15:50

1 Answer 1

1

You will need ProjectionOverlay so use Fluster2.packed.js instead of Fluster2.js. This was the reason you didn't get the alert message!

'map' isn't defined inside add_marker function so you should you should import that too!

The following code should work!

<!DOCTYPE html >
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>PHP/MySQL Example</title>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?
libraries=places&sensor=false"></script>
    <script type="text/javascript" src="Fluster2.packed.js"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobubble/src/infobubble.js"></script>
    <script type="text/javascript">

var customIcons = {
      restaurant: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      },
      bar: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      }
    };

var infoBubble;
  function initialize() {
    var latlng = new google.maps.LatLng(27.059125784374068,-37.6171875);
    var myOptions = {
    zoom: 3,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false,
    streetViewControl: false
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var fluster = new Fluster2(map);

      // Change this depending on the name of your PHP file
      downloadUrl("phpsqlajax_genxml3.php", function(data) {
        var xml = data.responseXML;
        var users = xml.documentElement.getElementsByTagName("user");
        for (var i = 0; i < users.length; i++) {
          var id = parseInt(users[i].getAttribute("id"));
          var name = users[i].getAttribute("name");
          var age = users[i].getAttribute("age");
          var type = users[i].getAttribute("type");
          var lat = parseFloat(users[i].getAttribute("lat"));
          var lng = parseFloat(users[i].getAttribute("lng"));
          var content = add_content(lat,lng,id,name,age,type,map); // pass in as Latitude, then Longitude
          fluster.addMarker(content);
        }
      });

var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
    var place = autocomplete.getPlace();
    if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
    } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);  // Why 17? Because it looks good.
    }

    var address = '';
    if (place.address_components) {
        address = [(place.address_components[0] &&
                    place.address_components[0].short_name || ''),
                   (place.address_components[1] &&
                    place.address_components[1].short_name || ''),
                   (place.address_components[2] &&
                    place.address_components[2].short_name || '')
                  ].join(' ');
    }
});

// Initialize Fluster
// This will set event handlers on the map and calculate clusters the first time.
    fluster.initialize();   
    }

 function add_content(lat,lng,id,name,age,type,map) {
 var icon = customIcons[type] || {};
 var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(lat,lng),
                    map: map,
                    icon: icon.icon,
                    title: name
                    });

    var contentString = '<div id="content">'+
        /*'<h2>'+name+'</h2>'+
        '<p><span class="myLabel" style="margin-right:10px">'+age+'</span>21</p>'+
        '<p><center><img src="'+id+'.jpg" class="bro_image"></center> </p>'+
        '<p><button class="button_inverse" style="padding-left: 23px; padding-right:23px; margin-left:31px;" href="#">Rate this Bro!</button></p>'+
        */'</div>';

   var infoBubble = new InfoBubble({
      maxWidth: 300,
      backgroundColor: '#dedddd',
      borderWidth: 2,
      borderColor: 'rgb(68, 68, 68)'
    });

    infoBubble.open(map, marker);

    var div = document.createElement('DIV');
    div.innerHTML = 'No pictures uploaded by this user.';

    infoBubble.addTab('Pictures', div);
    infoBubble.addTab('Personal', contentString);

    google.maps.event.addListener(marker, 'click', function() {
      if (!infoBubble.isOpen()) {
        infoBubble.open(map, marker);
      }
    });

return 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);
      alert(url);
      request.send(null);
    }
function doNothing() {}

  </script>

  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width: 800px; height: 550px"></div>
  </body>

</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.