Forum Thread: How Get Javascript Output into Database

Image via hostingpics.net

I found a javascript script that displays lacation (lattitude) if you allow it. Is theire a way to send this location data output directly to an email so I can see the location of persons that went on my site? Or to save the data in a text file into my website - save as a database? Here is the script :

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<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;
}
</script>

</body>
</html>

22 Responses

Google analytics does this for you.

Yes but google analitics gives an approximative result while this script gives a precise result, and that's what I find interresting

They may claim a precise result, but their results will be the same as Google's.

I tested the script, it does give my actual location. Are you shure google's result will be this acurate? I'm setting up Google analytics to check it out... :)

I think Google Analytics works by locating your approximate location via your IP address (hence, if you run a VPN, it'll say that you are in another location)

Yes it's aproximate, that's why I whant to be able to use my script... Any ideas?

How do you think this script gets your location? Magic?

ABRACADABRA ! Obiowsly not, the script askes permition to the browses to get location information. But i'm the one asking a question here ... so have you an idea for my problem?

This script simply uses the approximate location of the IP.

Ok but do you have an idea to send the data anyway? Could be usefull one day :)

You are reinventing the wheel. Google analytics calculates the location of every user whether they assent or not and stores it in a database for you.

Yes Google analitics is awsome, but I'm just having fun testing things like this. doesn't mather if it allready exists in google analitics, I would like to find a way to do it myself so I can use the same system to store any data than (not only localisation data)...

Uff if I try it then the coordinates are more than 100km away from my real location.

Exactly. The GetCurrentPosition function in javascript simply approximates the coordinates from the IP address.

So the location I get from the GetCurrentPosition function is the server location of my ISP where my IP is registered, isn't it?

No, the ISP's maintain a database of approximate location of every IP they assign. You can use and download it yourself. Take a look at mypost here.

Ah that post was actually in my "read later" folder ;)
I will give it a look, thanks ;)

Yeh I tried it again and I don't have my actual location -- I shurely had a bug with googlemaps yesterday because it showed me my position when I pasted the coordonates haha so I thought it worked... Damn --

Anyway eaven if I was wrong ( sorry OCCUPYT) i'm still interested in a way to send this kind of data through email so if anyone has some ideas I'm interested in it :)

If you would have checked the javascript documentation on GetCurrentPosition, you would see that it uses the IP.

Share Your Thoughts

  • Hot
  • Active