php - Pass resultset value to a javascript function -
i have 1 resultset, how can pass value javascript function ,and move internal pointer next row.
<?php $q=mysql_query("select * tbl_map")or die(mysql_error()); $i=0; ?> <script> $(document).ready(function() { // initialize 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 created - move marker.setposition(newpoint); } else { // marker not exist - create marker = new google.maps.marker({ position: newpoint, map: map }); } // center map on new position map.setcenter(newpoint); }); // call autoupdate() function every 5 seconds settimeout(autoupdate, 5000); } autoupdate(); }); </script>
i want fetch latitude , longitude database , pass autoupdate() can not move internal pointer ahead. how fix ?
try this: in method array of latitude
, longitude
assigned javascript variable json
in form of json data.
<script> // ---- php starts here fetch data , placed after <script> tag open // ---- , store variable in json format. <?php $q = mysql_query("select * 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 array of data `json` // ------ `var json` under scope of javascript echo 'var json = ' . json_encode($response) . ';'; } ?> // ---- php part ends here , javascript start // ---- testing `json` var console.log(json); console.log(json.length); $(document).ready(function() { // initialize 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 created - move marker.setposition(newpoint); } else { // marker not exist - create marker = new google.maps.marker({ position: newpoint, map: map }); } // center map on new position map.setcenter(newpoint); }); if (c == (json.length - 1)) { c = 0; } else { c++; } // call autoupdate() function every 5 seconds settimeout(autoupdate, 5000); } autoupdate(); }); </script>
Comments
Post a Comment