Geolocation and database

I want to design a website that take users’ location(altitude and longtitude)

and upload these parameter to mysql or MS sql.

Could anyone give me some hint how to accomplish this?

Thanks

Harry

You could make use of the navigator.geolocation object in JavaScript to obtain your visitor’s location and then use an XMLHttpRequest call to pass it to a server side script. Server side processing can then add the information into the database.

You must add some plugin offered by Google maps with satellite view easily navigate.

There are many tools available to help you do this. Some are free, others paid-for.

But your choice will depend on one vital factor: which server-side technology you are using to serve your pages. You mentioned MySQL, so I assume you are using PHP. In that case, the following links might be a good starting point:

http://freegeoip.appspot.com/

You can aslo do it in JavaScript, but that won’t help with updating a database:

http://www.maxmind.com/app/javascript_city

Regarding RaftyBooter’s response: “You must add some plugin offered by Google maps with satellite view easily navigate.” That’s not true. Google Maps is very useful for plotting locations on a visible map, but there are many better ways of retrieving the locations in a way that lets you save them in a database.

Mike

The server side solutions can only identify the location of the ISP that your visitor is using. Using the navigator.geolocation object is JavaScript can provide the exact location of your visitor if their browser is on a mobile device that has the ability to work out its current location.

Note that navigator.geolocation doesn’t work on IE8 or earlier - it does work on all modern browsers including those that are used by ALL mobile devices that also contain the ability to identify where they currently are.

The fallback for navigator.geolocation if the device doesn’t have GPS functionality built in is to use wifi to try to work out the location and only if that too doesn’t give a result does it fall back to identifying the location based on the IP address. So you’d still get the same inaccurate result for those devices that can’t tell exactly where they are as if you used a server side solution - Somewhere within a thousand miles or so instead of to within a hundred yards or so.

That maxmind JavaScript is still attempting to locate the IP address rather than the device and so can still be very inaccurate compared to using the object built into JavaScript itself.

See http://dev.w3.org/geo/api/spec-source.html for the specification on how this JavaScript object works.

Off Topic:

huh??? what’s this miles and yards stuff :eek:

Autralia was metricised back in 1970 from memory :lol:. It’s kilometres and metres now. I hope you still don’t go into the butcher’s asking for a pound (0.453kg ;)) of bbq snags :slight_smile:

[ot]What’s a “butcher’s”?:smiley: Didn’t they disappear about the same time that miles and furlongs and chains and feet did? (10 chains = 1 furlong - does that make them metric?)

I must have been accidentally assuming that the OP came from the USA (or whatever that other small part of the solar system - Ii mean country - is called where they still use antiquated measuring systems). Don’t know why I made that assumption since with 99.99999999+% of the solar system using the metric system it is far more likely that the Op does too.:rofl:[/ot]

You are right though that I shouldn’t have referred to yards as the geolocation object actually uses metres for the value it provides as the accuracy of the position (and of course yards vary in size with some people having much bigger ones than other people - and people who live in flats probably not having one at all).

ps. Just added the following example page on how to use the geolocation object in JavaScript - http://javascriptexample.net/bom12.php

[ot]

There are still butchers around where I live, but they are an endangered species :([/ot]

Database-wise storage is really easy if all you need to do is cache the values. If you need to say “how many people are near user X” then that is a different problem you’ll probably want some server support for. Sql 2008 or better has some geospatial bits built in, not sure if MySql has any at all. Some other non traditional non relational databases do pretty well in this field though, you might want to look there.

here is how I did it. I use the javascript to get the users geolocation(I tried on firefox, accuracy is within 10 meters). then use ajax to link to a php file and the ajax code pass the geolocation values to php, then php pass the geolocation value to database.

Ajax is critical here because when we get the geolocation values, it doesn’t refresh the browsers. (if refreshed, there is another pop up windows asking you to “are you sure to share the location”).

Thanks, guys


Did this get resolved, I think I am having the same problem.

I have created a php page which uses javascript to display the users geolocation which works fine.

Below that I have a form and what I want is for the lattitude and longittude to autofill in the values of the corresponding form fields.
hope that makes sense any help would be much appreciated.

Cheers Guys

Heres my code

<!DOCTYPE html>
<html>
<head>
<title>Relay Post DB V2</title>
</head>
<body>

<p>Latitude: <span id="lat">0.00</span> Longitude: <span id="lon">0.00</span></p>
    <p><a id="gmaps_link" href="http://maps.google.co.uk/" target="_blank">View on Google Maps</a></p>

    <script language="javascript">
       // show the position on the page and make a google maps link
       function showPosition(position) {
         var lat = position.coords.latitude;
         var lon = position.coords.longitude;
         document.getElementById("lat").innerHTML = lat;
         document.getElementById("lon").innerHTML = lon;
         var gmaps_url = "http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=" + lat + "+" + lon;
         document.getElementById("gmaps_link").href = gmaps_url;
       }
       // report errors to user
       function errorHandler(error) {
         switch (error.code) {
          case error.PERMISSION_DENIED:
            alert("Could not get position as permission was denied.");
            break;
          case error.POSITION_UNAVAILABLE:
            alert("Could not get position as this information is not available at this time.");
            break;
           case error.TIMEOUT:
             alert("Attempt to get position timed out.");
            break;
           default:
            alert("Sorry, an error occurred. Code: " + error.code + " Message: " + error.message);
            break;
           }
       }
       // check browser can support geolocation, if so get the current position
       if (navigator.geolocation) {
         navigator.geolocation.getCurrentPosition(showPosition, errorHandler);
       }
       else {
         alert("Sorry, your browser does not support geolocation services.");
       }
    </script>
         
<form action="insert.php" method="post">
Location <input type="text" name="location" value=""/><br>
Lattitude <input type="text" name="lattitude" value=""/><br>
Longitude <input type="text" name="longitude" value=""/><br>
Location <input type="text" name="location" value=""/><br>
Manual Location <input type="text" name="manuallocation" /><br>
IP <input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"/> <br>
Min Depth <input type="text" name="mindepth" /><br>
Max Depth <input type="text" name="maxdepth" /><br>
Min Height <input type="text" name="minheight" /><br>
Max Height <input type="text" name="maxheight" /><br>
Min Width <input type="text" name="minwidth" /><br>
Max Width <input type="text" name="maxwidth" /><br>
Picture <input type="text" name="picture" /><br>
Comments <input type="text" name="comments" /><br>
<input type="submit" />
</form>

</body>
</html>