Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    geolocation.getcurrentposition write to file?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Save Location</title>
    </head>
    <body on onload="getLocation()">
    <p id="demo"></p>
    <body>
    <script>
    var x=document.getElementById("demo");
    function getLocation()
      {
      if (navigator.geolocation)
        {
        navigator.geolocation.getCurrentPosition(showPosition);
        }
      else{x.innerHTML="Geolocation is not supported by this browser.";}
      }
    function showPosition(position)
      {
      x.innerHTML="Latitude: " + position.coords.latitude + 
      "<br>Longitude: " + position.coords.longitude;	
      }
      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>

    Hi. I'm pretty new to this stuff. I have figured out what I want JS to do, but I am not confident whether I can record this. For the demo, I am able to acquire user LONG/LAT and display it through x.innerHTML or alert. How would I go about recording and date stamping this to a file or a database? Would I need to toy around with php for this? Any help is greatly appreciated!

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,899
    Thanks
    56
    Thanked 542 Times in 539 Posts
    yeah, you're pretty much down to php and ajax or something similar - javascript can't really write to files (well, if you're IE only and you just want to write a file to the user's machine and they give you permission to do so, you can but really from what it sounds like that isn't the case).

    I made a google map that saves clicked locations to the database - you can see it here. That's kind of a different thing, but there is some overlap. I followed the tutorial here for setting up the php side, which I had no idea about and still only really vaguely understand how it works... but hey - the important thing is that it works, right?

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,899
    Thanks
    56
    Thanked 542 Times in 539 Posts
    another thing is that, as your code allows for, sometimes geolocation fails. And sometimes it works, but is way off. I don't know how fancy you want to get, but you can build error handling into the callback from getposition and have a fallback (such as ip address lookup) and if you want to incorporate a map into your page, you can allow the user to drag the estimated position to their real location.

    A couple of examples:

    draggable location with a choice of tracking techniques

    error handling - tries for geolocation, goes with IP lookup if that fails or user denies browser access to location

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,292
    Thanks
    10
    Thanked 583 Times in 564 Posts
    you can easily download a string as a file in modern browsers. but what good does that do? you probably want to collect the data, and for multi-device input, a server side language like php or node.js is the only way to go.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •