Where are you? A question a web server will want to ask of a browser or app client, especially on mobile devices. And modern browsers, through Javascript, can send that vital information to the server. It's really useful in a wide variety of geographic data feeds - I've been working on a prototype
listed building locator at http://melksh.am/lb which will find you the 15 nearest listed buildings to where you're located, tell you their direction, and let you move North South East or West to find more. This prototype already lists no fewer that 29,000 listed buildings within 25 miles of Melksham - so it covers Bradford-on-Avon, Chippenham, Malmesbury, Devizes, Trowbridge and Bath to name but a few ... links are provided for each building to the English Heritage site too.
So - how would you write this geolocation into you code?
I have a simple page
[here] which uses Javascript to ask the user to permit his location to be used (that's built in to modern browsers) then submits it up as a form. You'll find lots of sophisticated examples on line... I have tried to keep mine simple to show you what's happening.
1. When you load the page, it runs the Javascript:
<body onload='reportwhere()'>
2. The reportwere function runs the built in geolocation, passing the values it gets to two other functions which I've called nextpage and errhandler - the first if the user approves of his location being sent to the server, the second if he does not approve or geolocation isn't working:
function reportwhere() {
navigator.geolocation.getCurrentPosition(nextpage,errhandler);
}
3. These two functions save the status code, and location if it's available, into variables which are passed in to another function which calls up the next page
function nextpage(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
post_to_url("http://www.wellho.net/twapp/railinfo.php",{'scode':0, 'lati':latitude, 'longi':longitude})
}
function errhandler(problem) {
var whatswrong = problem.code;
post_to_url("http://www.wellho.net/twapp/railinfo.php",{'scode':whatswrong})
}
4. Here's that function that calls up the next page:
function post_to_url(url, params) {
var form = document.createElement('form');
form.action = url;
form.method = 'GET';
for (var i in params) {
if (params.hasOwnProperty(i)) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = i;
input.value = params[i];
form.appendChild(input);
}
}
form.submit();
}
Moving on, I've pointed that form at a short piece of PHP code (full source
[here] which will report on which railway stations you're nearest to, indicating which are busiest and adding a link to live departure boards. Again this is a prototype...
Illustration - Melksham's Spa Houses, dating from 1815. (written 2013-09-18, updated 2013-09-21)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
W604 - Web and Intranet - Beyond the Server [468] Stand alone PHP programs - (2005-10-18)
[2355] See this page in French - (2009-08-13)
H309 - PHP - Maps, Graphics and Geographics [320] Ordnance Survey - using a 'Get a map' - (2005-05-22)
[563] Merging pictures using PHP and GD - (2006-01-13)
[665] PHP Image viewing application - (2006-04-01)
[937] Display an image from a MySQL database in a web page via PHP - (2006-11-22)
[1104] Drawing dynamic graphs in PHP - (2007-03-09)
[1194] Drawing hands on a clock face - PHP - (2007-05-19)
[1389] Controlling and labelling Google maps via PHP - (2007-10-13)
[1390] Converting from postal address to latitude / longitude - (2007-10-13)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1628] Gant charts - drawing them with a PHP script - (2008-05-03)
[1724] addslashes v mysql_real_escape_string in PHP - (2008-07-27)
[1734] All around the world? - (2008-08-03)
[1752] Dynamic maps / geographics in PHP - (2008-08-13)
[1756] Ever had One of THOSE mornings? - (2008-08-16)
[1923] Making it all worthwhile - (2008-12-04)
[1956] Images for Christmas - (2008-12-21)
[2343] World Flags in your PHP pages - (2009-08-10)
[2361] Geocoding - converting address to latitude / Longitude with PHP via Google - (2009-08-14)
[2390] Dynamic / changing images on your web page - (2009-09-01)
[2583] Reducing image size on digital photos - PHP - (2010-01-17)
[2675] Redirecting to your main domain for correct security keys - (2010-03-13)
[2729] Uploading a document or image to its own URL via a browser - (2010-04-18)
[2939] Protecting your images from use out of context - (2010-08-29)
[3027] Server logs - drawing a graph of gathered data - (2010-11-03)
[3133] An image from a website that occasionally comes out as hyroglyphics - (2011-01-14)
[3197] Finding and diverting image requests from rogue domains - (2011-03-08)
[3211] Computer Graphics in PHP - World (incoming data) to Pixel (screen) conversion - (2011-03-24)
[3447] Needle in a haystack - finding the web server overload - (2011-09-18)
[3536] UK Mapping Data - and more to come - under government Open Data measures - (2011-12-03)
[3584] QR codes - graphics images that provide quick phone links - (2012-01-18)
[3734] QR codes with marketing logos embedded - (2012-05-16)
[3817] Fpdf - generating .pdf documents easily from your PHP program - (2012-07-24)
[4365] The changing face of Christmas - (2014-12-26)
[4437] Adding a PHP build option, rotating an image based on camera data, and a new look at thumbnails in PHP - (2015-02-22)
[4655] Image indexer / thumbnail display scripts in PHP - (2016-02-25)
Some other Articles
Welcome to Salford and OldhamGrumbling about trains? Buses can be far worse!Exceptionally, I sign a petition - on a life and death issue for Irish dogsChanging transport and destinations - looking forward to a good futureWhere are you? How to write a geosensitive applicationSetting and publishing your hours to suit your customer baseInternational and off island travel from MelkshamAn alternative format for a new train service timetableWhat would the extra trains from Melksham mean to your journey?Train and Rail Travel - who runs it and where do I ask questions?