Retrieve values from mssql to add new markers to Google Maps

Hi there,

Can anybody help me with this code… Basically I want to output a Google map with marker locations from my MSSQL databse. I am connecting to the database and outputting data from the database with out any issues. The values in my database that need to be used are latitude and longitude

I just need some help getting the latitude and longitude information from the database into an array that can feed through to “var markers = createMarker”. Can anyone suggest any code or advice?

<!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">
  <head>
    <meta http-equiv="content-type" content="text/html;
    charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=ABQIAAAAFdAFQ5eUeLToBjop7wxvVRQwhZZS-c1L0GQSo2zewspgIUCbcBQLESvgR7pk_2Z2FMWsfROoCbyJrw" type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(
        document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.addMapType(G_SATELLITE_3D_MAP);
        map.setCenter(
        new GLatLng(37.4419, -122.1419), 13);
        function createMarker(point, text, title) {
          var marker =
          new GMarker(point,{title:title});
          return marker;
        }
        <?php


        $points = Array(
         "37.4389, -122.1389",
        "37.4419, -122.1419",
         "37.4449, -122.1449");
        foreach ($points as $key => $point) {
        ?>

        <!-- ADD NEW MARKERS FROM A DATABASE -->

        var marker = createMarker(  
        new GLatLng(<?php echo $point ?>)
        );
        map.addOverlay(marker);
        <?php } ?>
      }
    }
    //]]>
    </script>

  </head>
  <body onload="load()" onunload="GUnload()">


    <div id="map"
    style="width: 500px; height: 300px"></div>

    <?php
    /*data base connection */


    /* SQL query */
$tsql = ("SELECT gps_lat
      ,gps_long
  FROM location");

  $result = sqlsrv_query($conn,$tsql);

if( $result === false)
{
     echo "Error in query preparation/execution.\
";
     die( print_r( sqlsrv_errors(), true));
}



/*Display the table */
// keeps getting the next row until there are no more to get
while($points = sqlsrv_fetch_array( $result )) {
    echo $points['gps_lat'];
    echo ',';
    echo $points['gps_long'];
    echo "</br>";
}


/* Close the connection. */
sqlsrv_close( $conn);


?>
  </body>
</html>

I have tried to create the new markers by using the below method, but I have been unsuccessful. Can anyone point out anything wrong with the code below or give me any advice?

//<![CDATA[
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(
        document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.addMapType(G_SATELLITE_3D_MAP);
        map.setCenter(
        new GLatLng(37.4419, -122.1419), 13);
        function createMarker(point, text, title) {
          var marker =
          new GMarker(point,{title:title});
          return marker;
        }
        <?php
        /*data base connection */


        /* Connect using Windows Authentication */       
        $conn = sqlsrv_connect($serverName, $connectionOptions);

        /* Check whether connnection is established */
        if($conn === false)
        {
            die(print_r(sqlsrv_errors(), true));
        }

        /* SQL query */
        $tsql = ("SELECT TOP 10 gps_lat
                ,gps_long
                FROM location");

        $result = sqlsrv_query($conn,$tsql);

        if( $result === false)
        {
            echo "Error in query preparation/execution.\
";
            die( print_r( sqlsrv_errors(), true));
        }

/*
BELOW IS THE ADJUSTED WHILE LOOP
*/
while($row = sqlsrv_fetch_array( $result )) {
    $lat = $row['gps_lat'];
    $long = $row['gps_long'];

        ?>      
        var marker = createMarker(  
        new GLatLng(<?php echo $Lat.','.$Long;?>)
        );
        map.addOverlay(marker);
        <?php } ?>
      }
    }
    //]]>
    </script>

I thought it would be fairly straightforward but I can’t seem to get my head around it. If there is any information that is unclear… please let me know.

Cheers,

Neil

Try separating out the process, for instance, a simple function to get the lat/lon.


<?php
function getGMapMarkers(){
  /* Add whatever code you need here to get this data*/
  return array(
    array(
      'gps_lat' => 37.4419,
      'gps_lon' => -122.1419
    ),
    array(
      'gps_lat' => 37.4419,
      'gps_lon' => -122.1419
    ),
    array(
      'gps_lat' => 37.4419,
      'gps_lon' => -122.1419
    ),
  );
}
?>
<html>
  <head>
    <title>demo</title>
  </head>
  <body>
    <script type="text/javascript">
      <?php foreach(getGMapMarkers() as $marker): ?>
        var marker = new GLatLng(<?php echo $marker['gps_lat']; ?>, <?php echo $marker['gps_lon']; ?>), 13);
      <?php endforeach; ?>
    </script>
  </body>
</html>

Thank you very much for your advice AnthonySterling! I have managed to resolve the issue with a couple tweaks of your code… Hope you have a good bank holiday weekend!

Cheers

Neil