Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-07-2013, 04:50 AM   PM User | #1
agentv
New to the CF scene

 
Join Date: Feb 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
agentv is an unknown quantity at this point
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!
agentv is offline   Reply With Quote
Old 02-07-2013, 02:13 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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?
xelawho is offline   Reply With Quote
Old 02-07-2013, 02:44 PM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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
xelawho is offline   Reply With Quote
Old 02-07-2013, 10:03 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:24 AM.


Advertisement
Log in to turn off these ads.