title: Load markers to google map from json php
body: <p>I am trying to load some markers to google maps for my webpage. </p>
  
  <p>I have all markers in a DB and I take them by a php script. This script print all data in DB as json. Something like this:</p>
  
  <pre><code>&lt;?php
     require_once("phpsqlajax_dbinfo.php"); //info for DB connection
  
  
     // Opens a connection to a MySQL server
     $connection=mysql_connect ('localhost', $username, $password);
     if (!$connection) {
       die('Not connected : ' . mysql_error());
     }
  
     // Set the active MySQL database
     $db_selected = mysql_select_db($database, $connection);
     if (!$db_selected) {
      die ('Can\'t use db : ' . mysql_error());
     }
  
    // Select all the rows in the markers table
    $query = "SELECT coordY, coordX FROM contenedor WHERE peso = 12";
    $result = mysql_query($query);
    if (!$result) {
      die('Invalid query: ' . mysql_error());
    }
  
  
  
    $x = 0;
    while ($row = @mysql_fetch_assoc($result)){
      $data[$x] = array("lat" =&gt; $row['coordY'], "lng" =&gt; $row['coordX']);
      $x++;
   }
   echo json_encode($data);
   ?&gt;
  </code></pre>
  
  <p>My json file should look like this:
  [{"lat":"A","lng":"B"},{"lat":"C","lng":"D"},{"lat":"E","lng":"F"}] //Characters are lat and long...</p>
  
  <p>After that, in other file (my index.html) I create map, like:</p>
  
  <pre><code> var map = new GMap2(document.getElementById("map_canvas"));
      map.setCenter(new GLatLng(X, Y), 13); //X and Y are my lat and long
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
   ...
  </code></pre>
  
  <p>I want to make a function called addMarket to load all my markers. But I am not able to get the info from that file from javascript. I though something like this:</p>
  
  <pre><code>for (var i = 0;i &lt; file.length; i += 1) {
      var lat = file[i].lat;
      var lon = file[i].lng;
      addMarker(lat,lon);
  };
  
  function addMarker(lat,lng){
  var latlng = new google.maps.LatLng(lat,lng);  
  var marker = new google.maps.Marker({position: latlng, map: map});
  </code></pre>
  
  <p>But I don't kow how I should read values or get all of them in an array.</p>
  
  <p>Any idea?</p>
  
  <p>Other question I have is: is it possible to upload markers when I am watching the map? For example, if one of my markers change during watching, I want to get the new value (for example, changing color..) I have addMarker function inside initialize function, should I get it outside? </p>
  
  <p>thanks!</p>