Thomas Prendergast
Thomas Prendergast

Reputation: 11

Send current gps coordinates to mySQL database using google maps api

I am trying to build a golf app in jquery where I can send the current gps coordinates to a mySQL database using a button.

The problem is that I am new to php and I am trying to find code that i can use. I have built a database:

  1. ID int 3 autoincrement
  2. LAT float 10
  3. LON float 10

Can someone please help with some html and php code?

<!DOCTYPE html>
<html>
<body>
    <div data-role="page" id="page1">
        <div data-theme="b" data-role="header" data-position="fixed">
            <h5>
                Play 9 holes
            </h5>
        </div>
    </div>
<p id="demo">Click the button to get your position:</p>
<button onclick="getLocation()">Try It</button>
<div id="mapholder"></div>
<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition,showError);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }

 function showPosition(position)
  {
  var latlon=position.coords.latitude+","+position.coords.longitude;

  var img_url="http://maps.googleapis.com/maps/api/staticmap?center="
   +latlon+"&zoom=14&size=400x400&sensor=false";
   document.getElementById("mapholder").innerHTML="<img src='"+img_url+"' />";
   }

 function showError(error)
  {
   switch(error.code) 
     {
   case error.PERMISSION_DENIED:
      x.innerHTML="User denied the request for Geolocation."
       break;
     case error.POSITION_UNAVAILABLE:
       x.innerHTML="Location information is unavailable."
        break;
     case error.TIMEOUT:
       x.innerHTML="The request to get user location timed out."
        break;
     case error.UNKNOWN_ERROR:
         x.innerHTML="An unknown error occurred."
       break;
     }
   }
 </script>
 </body>
 </html>

this code shows current location but there has to be a request function that can send position.coords.latitude+","+position.coords.longitude to database?

Upvotes: 1

Views: 9725

Answers (1)

Mano Marks
Mano Marks

Reputation: 8819

I'm really not sure what you're trying to do, but here's some articles in the Google Maps API site on using PHP and MySQL with the Google Maps API:

https://developers.google.com/maps/articles/phpsqlsearch_v3

https://developers.google.com/maps/articles/phpsqlajax_v3

https://developers.google.com/maps/articles/phpsqlinfo_v3

https://developers.google.com/maps/articles/phpsqlgeocode

Upvotes: 1

Related Questions