Initially, I had a code that worked:
function get_coords(callback) {
//If HTML5 Geolocation Is Supported In This Browser
if (navigator.geolocation)
{
//Use HTML5 Geolocation API To Get Current Position
navigator.geolocation.getCurrentPosition(function(position)
{
//Get Latitude From Geolocation API
var lat = position.coords.latitude;
//Get Longitude From Geolocation API
var lng = position.coords.longitude;
callback(["coords", lat, lng]);
},
function()
{
callback(["geoloc_deactivated"]);
});
}
else
{
callback(["geoloc_not_supported"]);
}
}
Then I wanted to integrate the solution of this page: http://jsfiddle.net/CvSW4/
And now my code looks like this and it doesn't work anymore (lost in the callback functions):
function get_coords(callback)
{
//If HTML5 Geolocation Is Supported In This Browser
if (navigator.geolocation)
{
//~ //Use HTML5 Geolocation API To Get Current Position
navigator.geolocation.getCurrentPosition(
successCallback,
errorCallback_highAccuracy,
{maximumAge:600000, timeout:5000, enableHighAccuracy: true}
);
}
else
{
callback(["geoloc_not_supported"]);
}
}
function successCallback(position)
{
var lat = position.coords.latitude;
var lng = position.coords.longitude;
position(["coords", lat, lng]);
}
function errorCallback_highAccuracy(callback_high_accuracy)
{
if (error.code == error.TIMEOUT)
{
// Attempt to get GPS loc timed out after 5 seconds,
// try low accuracy location
navigator.geolocation.getCurrentPosition(
successCallback,
errorCallback_lowAccuracy,
{maximumAge:600000, timeout:10000, enableHighAccuracy: false});
return;
}
if (error.code == 1)
callback_high_accuracy(["perm_denied"])
else if (error.code == 2)
callback_high_accuracy(["pos_unavailable"])
}
function errorCallback_lowAccuracy(callback_low_accuracy)
{
if (error.code == 1)
callback_low_accuracy(["perm_denied"])
else if (error.code == 2)
callback_low_accuracy(["pos_unavailable"])
else if (error.code == 3)
callback_low_accuracy(["timeout"])
}
I get the error:
Uncaught TypeError: object is not a functioncampaigns.min.js:37 successCallback
The line is:
position(["coords", lat, lng]);
How to make the callback work?
I just found out how to make it work.
The code below tracks the user's location with his/her ip address (html5 feature). It first uses high accuracy for 5 seconds then, in case of error, tries with low accuracy for 10 seconds.
If there is an error, the callback gives the error. If the geolocation worked, it gives the coordinates.
function get_coords(callback) {
//If HTML5 Geolocation Is Supported In This Browser
if (navigator.geolocation)
{
//Use HTML5 Geolocation API To Get Current Position
// try high accuracy location for 5 seconds
navigator.geolocation.getCurrentPosition(function(position)
{
//Get Latitude From Geolocation API
var lat = position.coords.latitude;
//Get Longitude From Geolocation API
var lng = position.coords.longitude;
window.console&&console.log('coords : latitude=' + lat + ", longitude=" + lng);
callback(["coords", lat, lng]);
},
function(error)
{
if (error.code == error.TIMEOUT)
{
// Attempt to get GPS loc timed out after 5 seconds,
// try low accuracy location for 10 seconds
navigator.geolocation.getCurrentPosition(function(position)
{
//Get Latitude From Geolocation API
var lat = position.coords.latitude;
//Get Longitude From Geolocation API
var lng = position.coords.longitude;
window.console&&console.log('coords : latitude=' + lat + ", longitude=" + lng);
callback(["coords", lat, lng]);
},
function(error)
{
if (error.code == 1)
{
window.console&&console.log('low accuracy geoloc_deactivated');
callback(["geoloc_deactivated"]);
}
else if (error.code == 2)
{
window.console&&console.log('low accuracy position_unavailable');
callback(["position_unavailable"]);
}
else if (error.code == 3)
{
window.console&&console.log("low accuracy timeout");
callback(["timeout"]);
}
},
{
maximumAge:600000,
timeout:10000,
enableHighAccuracy: false
});
}
if (error.code == 1)
{
window.console&&console.log('high accuracy geoloc_deactivated');
callback(["geoloc_deactivated"]);
}
else if (error.code == 2)
{
window.console&&console.log('high accuracy position_unavailable');
callback(["position_unavailable"]);
}
},
{
maximumAge:600000,
timeout:5000,
enableHighAccuracy: true
});
}
else
{
window.console&&console.log("geoloc_not_supported");
callback(["geoloc_not_supported"]);
}
}
How to use the code : on the page you want to track the user, use this javascript
call:
get_coords(function(callback)
{
if (callback[0]=="coords")
{
user_coords=[callback[1],callback[2]];
//do stuff
}
else if(callback[0]=="position_unavailable")
{
//do stuff
}
else if(callback[0]=="timeout")
{
//do stuff
}
else if(callback[0]=="geoloc_deactivated")
{
//do stuff
}
else if(callback[0]=="geoloc_not_supported")
{
//do stuff
}
});