Getting information from database and giving it to javascript The main thing I want to achieve is that my module makes a google map with markers.

For now im doing this:

drupal_add_js('https://maps.googleapis.com/maps/api/js?key=AIzaSyDcl28Qcn9GTi02rRtQaLgVS4GWZbGcQ8E', 'external');

$locations = db_query('SELECT * FROM {locations}')-&gt;execute();

$script = '&lt;script&gt;
     function initialize() {

    var mapOptions = {
      center: { lat: 56.9714744, lng: 24.1291624},
      zoom: 9
    };
    var map = new google.maps.Map(document.getElementById(\'map-canvas\'),
        mapOptions);

    var locations = [';

    for($i = 0; $i &lt; $locations-&gt;length;$i++){
        $string = '['.$locations[$i]['locationsTitle'].','.$locations[$i]['locationsLat'].','.$locations[$i]['locationLng'].'],';
        $script = $script.$string;
    }

    $script = $script.'];
    for (var i = 0; i &lt; locations.length; i++) {

      var loc = new google.maps.LatLng(locations[i][1], locations[i][2]);

      var marker = new google.maps.Marker({
        position: loc,
        map: map,
        title: locations[i][0]
      });
    };

  }

  google.maps.event.addDomListener(window, \'load\', initialize);

&lt;/script&gt;
';

$html = '
&lt;div id="map-wrapper" style="width:100%;height:500px;margin:auto"&gt;
    &lt;ul id="map-list"&gt;&lt;/ul&gt;
    &lt;div id="map-canvas" style="width:100%;height:100%;margin:auto&gt;&lt;/div&gt;
&lt;/div&gt;
';

return array('#markup' =&gt; $script.$html);


As you can see in the third line of code im trying to get information from the database that I have saved. The problem is I dont know how would i get it properly. And then I want to pass it down in the var locations part in the script..

I have tried finding some tutorials where they explain how would this work but most of them is just how to show some posts or somthing like that and that doesnt help me.

Thanks for help already! :)

// EDIT

This is the script that i have now in js file

(function($) {
     Drupal.behaviors.myModule = {
       attach: function(context, settings) {

          function initialize() {

          var mapOptions = {
             center: { lat: 56.9714744, lng: 24.1291624},
             zoom: 9
          };
          var map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);

    var locations = [
                    ['Mājas',56.956344, 24.197409],
                    ['Domina',56.966058, 24.162004]
                    ];

    for (var i = 0; i &lt; locations.length; i++) {

      var loc = new google.maps.LatLng(locations[i][1], locations[i][2]);

      var marker = new google.maps.Marker({
        position: loc,
        map: map,
        title: locations[i][0]
      });
    };

  }

  //google.maps.event.addDomListener(window, 'load', initialize);

}


}
 )(jQuery);

I have the "google.maps.event.addDomListener(window, 'load', initialize);" commented becouse I think i dont need it..? And becouse there is no function initialize anymore? Or should i add that back?

The problem now is that my script seems to be added.. But it doesnt seem to be run becouse the google map dosnt show up..
You should never need to build javascript with PHP, there's just no need (especially in Drupal, which already has a mechanism).

What you should do instead is put your JS in an external file, then use Drupal's JS settings to pass the data. So the JS file might look like:

(function($) {
  Drupal.behaviors.myModule = {
    attach: function(context, settings) {
      var locations = Drupal.settings.myModule.locations;
      // Do something with the locations var.
    }
  }
)(jQuery);


And in your page callback or whatever:

$settings['myModule']['location'] = db_query('SELECT * FROM {locations}')-&gt;fetchAll();
drupal_add_js($settings, 'setting');