{"id":42,"date":"2023-12-30T10:09:45","date_gmt":"2023-12-30T16:09:45","guid":{"rendered":"https:\/\/anthony.sleck.us\/?p=42"},"modified":"2023-12-30T10:23:44","modified_gmt":"2023-12-30T16:23:44","slug":"google-maps-api-with-php-and-mysql","status":"publish","type":"post","link":"https:\/\/anthony.sleck.us\/?p=42","title":{"rendered":"Google Maps API with PHP and MySQL"},"content":{"rendered":"\n<p>Posting data from an SQL server to a webpage using the Google Maps API is a lot simpler than you may think. I am going to outline some of the things that I learned in getting this to all work.<\/p>\n\n\n\n<p><strong>Prerequsites<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Please note that I am running Ubuntu Server 22.04 in a VM<ul><li>Apache Server<\/li><\/ul><ul><li>MySQL Server<\/li><\/ul><ul><li>WebMin &#8211; Optional<\/li><\/ul><ul><li>MyPHPAdmin \u2013 Optional<\/li><\/ul>\n<ul class=\"wp-block-list\">\n<li>Working knowledge of PHP, HTML, SQL<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Database Structure<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Database_name \u2013 dealers\u2019 choice for database name\n<ul class=\"wp-block-list\">\n<li>Database_table \u2013 dealers\u2019 choice for table name\n<ul class=\"wp-block-list\">\n<li>Columns<ul><li>id<ul><li>Type: int<\/li><\/ul><ul><li>Autoincrement<\/li><\/ul><ul><li>Primary<\/li><\/ul><\/li><\/ul><ul><li>node_id<ul><li>Type: int<\/li><\/ul><\/li><\/ul><ul><li>pt_id<ul><li>Type: int<\/li><\/ul><\/li><\/ul><ul><li>lat<ul><li>Type: decimal (30,6)<\/li><\/ul><\/li><\/ul>\n<ul class=\"wp-block-list\">\n<li>lng\n<ul class=\"wp-block-list\">\n<li>Type: decimal (30,6)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>WWW Structure<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>www root<ul><li>img<ul><li>icon1.png<\/li><\/ul><ul><li>mark1.png<\/li><\/ul><ul><li>Note that cons and mark should be roughly 96*96 pixels and can be anything you would like if it is a png with a transparent background.<\/li><\/ul><\/li><\/ul>\n<ul class=\"wp-block-list\">\n<li>index.php \u2013 dealers\u2019 choice for page name<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>GUI Page Code<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;?php\n\/\/ database connection settings\n$servername = &#039;********&#039;;\n$username = &#039;********&#039;;\n$password = &#039;********&#039;;\n$dbname = &#039;*******&#039;;\n\n\/\/ create connection to database\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ check database connection\nif ($conn-&gt;connect_error) {\n  die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);\n}\n\n\/\/ start node 1\n$query1 = &quot;SELECT pt_id, lat, lng FROM node_test_1 WHERE node_id = 1 ORDER BY id DESC LIMIT 1&quot;;\n$result1 = $conn-&gt;query($query1);\n\nif ($result1-&gt;num_rows &gt; 0) {\n    $row = $result1-&gt;fetch_assoc();\n    $latitude1 = $row&#x5B;&#039;lat&#039;];\n    $longitude1 = $row&#x5B;&#039;lng&#039;];\n} else {\n    echo &quot;No data found&quot;;\n}\n\/\/ end node 1\n\n$conn-&gt;close();\n?&gt;\n\n&amp;lt;!DOCTYPE html&gt;\n&amp;lt;html&gt;\n&amp;lt;head&gt;\n  &amp;lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n  &amp;lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;style.css&quot;&gt;\n  &amp;lt;meta http-equiv=&quot;refresh&quot; content=&quot;30&quot;&gt;\n  &amp;lt;title&gt;Map | View&amp;lt;\/title&gt;\n    &amp;lt;script src=&quot;https:\/\/maps.googleapis.com\/maps\/api\/PASTE_YOUR_API_KEY_HERE&amp;amp;callback=initMap&quot;\n            async defer&gt;\n    &amp;lt;\/script&gt;\n    &amp;lt;script&gt;\n              function initMap() {\n                var node1LatLng = {lat:&amp;lt;?php echo $latitude1; ?&gt;, lng:&amp;lt;?php echo $longitude1; ?&gt;};\n                var map = new google.maps.Map(document.getElementById(&#039;map&#039;),{\n                  zoom: 15,\n                  center: node1LatLng,\n                  mapTypeId: google.maps.MapTypeId.TERRAIN\n                });\n\n                mark = &#039;img\/mark1.png&#039;;\n                var marker = new google.maps.Marker({\n                  position: node1LatLng,\n                  map: map,\n                  icon: mark,\n                  title: &#039;Node 1 Current GPS Position.&#039;,\n                  animation: google.maps.Animation.DROP\n                });\n              }\n    &amp;lt;\/script&gt;\n  &amp;lt;\/head&gt;\n&amp;lt;body&gt;\n  &amp;lt;nav&gt;\n    &amp;lt;ul&gt;\n      &amp;lt;p&gt;&amp;lt;li&gt;&amp;lt;a href=&quot;#&quot;&gt;&amp;lt;img src=&quot;img\/logout.png&quot;&gt;&amp;lt;\/a&gt;&amp;lt;\/li&gt;&amp;lt;\/P&gt;\n    &amp;lt;\/ul&gt;\n  &amp;lt;\/nav&gt;\n  &amp;lt;div&gt;\n    &amp;lt;p&gt;This page Automatically refreshes every 30 seconds!&amp;lt;\/p&gt;\n  &amp;lt;\/div&gt;\n\n  &amp;lt;div class=&quot;outer-scontainer&quot;&gt;\n    &amp;lt;div class=&quot;row&quot;&gt;\n      &amp;lt;form class=&quot;form-horizontal&quot; action=&quot;&quot; method=&quot;post&quot; name=&quot;frmCSVImport&quot; id=&quot;frmCSVImport&quot; enctype=&quot;multipart\/form-data&quot;&gt;\n      &amp;lt;\/form&gt;\n    &amp;lt;\/div&gt;\n    &amp;lt;div id=&quot;map&quot; style=&quot;height: 600px; width: 100%;&quot;&gt;&amp;lt;\/div&gt;\n&amp;lt;\/body&gt;\n&amp;lt;\/html&gt;\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Posting data from an SQL server to a webpage using the Google Maps API is a lot simpler than you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/anthony.sleck.us\/index.php?rest_route=\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/anthony.sleck.us\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anthony.sleck.us\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anthony.sleck.us\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/anthony.sleck.us\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=42"}],"version-history":[{"count":4,"href":"https:\/\/anthony.sleck.us\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/anthony.sleck.us\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/anthony.sleck.us\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anthony.sleck.us\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anthony.sleck.us\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}