将结果集值传递给javascript函数

时间:2016-02-08 07:43:35

标签: javascript php

我有一个结果集, 如何将其值传递给javascript函数,并将内部指针移动到下一行。

<?php 
$q=mysql_query("select * from tbl_map")or die(mysql_error());
$i=0;
?>

<script>
$(document).ready(function() {
// Initialize the Google Maps API v3
var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 15,
  mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = null;

function autoUpdate() {

<?php 
mysql_data_seek($q,$i);
$row=mysql_fetch_assoc($q);
$i++;
?>

lat=<?php echo $row['latitude']; ?>;

long=<?php echo $row['longitude']; ?>;

  navigator.geolocation.getCurrentPosition(function(position) {  
    var newPoint = new google.maps.LatLng(lat, 
                                          long);

    if (marker) {
      // Marker already created - Move it
      marker.setPosition(newPoint);
    }
    else {
      // Marker does not exist - Create it
      marker = new google.maps.Marker({
        position: newPoint,
        map: map
      });
    }

    // Center the map on the new position
    map.setCenter(newPoint);
  }); 

  // Call the autoUpdate() function every 5 seconds
  setTimeout(autoUpdate, 5000);

}

autoUpdate();
      });
</script> 

我想从数据库中获取经度和纬度并传递给autoupdate() 但我无法向前移动内部指针。 怎么解决?

1 个答案:

答案 0 :(得分:1)

试试这个:在此方法中,latitudelongitude的数组以json数据的形式分配给javascript变量json

    <script>

       // ---- PHP Starts here which fetch data and is placed immediately after <script> tag open
       // ---- and store to a variable in json format.

       <?php

            $q = mysql_query("select * from tbl_map")or die(mysql_error());

            $response = array();

            for($i = 0; $i < mysql_num_rows($q) ; $i++ )
            {

                mysql_data_seek($q, $i);

                $row = mysql_fetch_assoc($q);

                array_push( $response, array(
                    'lat'=>$row['latitude'],
                    'long'=>$row['longitude']
                ));

                // ------ Here php assigns the array of data to `json`
                // ------ `var json` is under scope of javascript
                echo 'var json = ' . json_encode($response) . ';';

            }

       ?>

    // ---- PHP part ends here and javascript start
    // ---- Testing the `json` var

    console.log(json);
    console.log(json.length);


    $(document).ready(function() {

        // Initialize the Google Maps API v3
        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var c = 0;

        var marker = null;

        function autoUpdate() {

                console.log(json[c].lat);
                console.log(json[c].long);

                navigator.geolocation.getCurrentPosition( function(position)
                {

                    var newPoint = new google.maps.LatLng( json[c].lat, json[c].long );

                    if (marker) {
                        // Marker already created - Move it
                        marker.setPosition(newPoint);
                    }
                    else {
                        // Marker does not exist - Create it
                        marker = new google.maps.Marker({
                            position: newPoint,
                            map: map
                        });
                    }

                    // Center the map on the new position
                    map.setCenter(newPoint);
                });

            if (c == (json.length - 1)) { c = 0; } else { c++; }

            // Call the autoUpdate() function every 5 seconds
            setTimeout(autoUpdate, 5000);

        }

        autoUpdate();

    });

    </script>