Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to return php array from mysql into google geolocation
    primarykey
    data
    text
    <p>I have data in mysql database and I want to be able to read that data in php array and use it for google geolocation.</p> <p>So for this example I want to use only <code>SELECT *</code> statement, as I will not be bothering you with some parameters...</p> <p>What I want to achieve is to get marked line from point A to point B ( depending where GPS location was saved )</p> <p>This is the link what I want to have on my map: <a href="https://google-developers.appspot.com/maps/documentation/javascript/examples/polyline-simple" rel="nofollow noreferrer">POLYLINE</a> in this example there are only 4 data in the array I want to have it more.</p> <p>So now we can get back to the code. This is my PHP script for connecting to mysql. I have been using mysqli as I will have stored procedures later one in the database, so don't get confused.</p> <pre><code>class dbMySql { static function Exec($query) { // open database $conn = mysqli_connect( $GLOBALS['cfg_db_Server'], $GLOBALS['cfg_db_User'], $GLOBALS['cfg_db_Pass'] ); if($conn === false) { throw new Exception(mysqli_connect_error()); } mysqli_select_db($conn,$GLOBALS['cfg_db_Name']); $result = mysqli_query($conn,$query); if(is_bool($result) &amp;&amp; !$result) { $error = mysqli_error($conn); mysqli_close($conn); throw new Exception($error); } mysqli_close($conn); return $result; } } </code></pre> <p>How to connect this php script to code that is example on google API page, and insert my array values when button is clicked instead of the fixed ones:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta name="viewport" content="initial-scale=1.0, user-scalable=no" /&gt; &lt;script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=KEY&amp;sensor=true"&gt; &lt;/script&gt; &lt;script type="text/javascript"&gt; function initialize() { var myLatLng = new google.maps.LatLng(0, -180); var myOptions = { zoom: 3, center: myLatLng, mapTypeId: google.maps.MapTypeId.TERRAIN }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var flightPlanCoordinates = [ new google.maps.LatLng(37.772323, -122.214897), new google.maps.LatLng(21.291982, -157.821856), new google.maps.LatLng(-18.142599, 178.431), new google.maps.LatLng(-27.46758, 153.027892) ]; var flightPath = new google.maps.Polyline({ path: flightPlanCoordinates, strokeColor: "#FF0000", strokeOpacity: 1.0, strokeWeight: 2 }); flightPath.setMap(map); } &lt;/script&gt; &lt;/head&gt; &lt;body onload="initialize()"&gt; &lt;div id="map_canvas" style="width:100%; height:80%"&gt;&lt;/div&gt; &lt;div&gt;&lt;button type="button"&gt;Click Me!&lt;/button&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>EDIT:</p> <p>this is what I get when I do your code: <img src="https://i.stack.imgur.com/C00wn.png" alt="error"></p> <p>EDIT2:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta name="viewport" content="initial-scale=1.0, user-scalable=no" /&gt; &lt;style type="text/css"&gt; html { height: 100% } body { height: 100%; margin: 0; padding: 0 } #map_canvas { height: 100% } &lt;/style&gt; &lt;script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=key&amp;sensor=true"&gt; &lt;/script&gt; &lt;script type="text/javascript"&gt; function initialize() { var myLatLng = new google.maps.LatLng(0, 180); var myOptions = { zoom: 3, center: myLatLng, mapTypeId: google.maps.MapTypeId.TERRAIN }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var flightPlanCoordinates = [ &lt;?php echo implode(',', $coordinates) ?&gt; ]; ]; var flightPath = new google.maps.Polyline({ path: flightPlanCoordinates, strokeColor: "#FF0000", strokeOpacity: 1.0, strokeWeight: 2 }); flightPath.setMap(map); } &lt;/script&gt; &lt;/script&gt; &lt;?PHP class dbMySql { static function Exec($query) { // open database $conn = mysqli_connect('localhost','root','*****'); if($conn === false) { throw new Exception(mysqli_connect_error()); } mysqli_select_db($conn,'data_gps'); $result = mysqli_query($conn,$query); if(is_bool($result) &amp;&amp; !$result) { $error = mysqli_error($conn); mysqli_close($conn); throw new Exception($error); } mysqli_close($conn); return $result; } } $coordinates = array(); $result = dbMySql::Exec('SELECT lat,lng FROM data'); while ($row = mysqli_fetch_assoc($result)) $coordinates[] = 'new google.maps.LatLng(' . $row['lat'] . ', ' . $row['lng'] . ')' ; ?&gt; &lt;/script&gt; &lt;/head&gt; &lt;body onload="initialize()"&gt; &lt;div id="map_canvas" style="width:100%; height:80%"&gt;&lt;/div&gt; &lt;div&gt;&lt;button type="button"&gt;Click Me!&lt;/button&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload