navigator.geolocation 和回调的回调
navigator.geolocation and callback of callback
最初,我有一个有效的代码:
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"]);
}
}
然后我想整合这个页面的解决方案:
http://jsfiddle.net/CvSW4/
现在我的代码看起来像这样,它不再工作了(在回调函数中丢失):
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"])
}
我收到错误:
Uncaught TypeError: object is not a functioncampaigns.min.js:37 successCallback
行是:
position(["coords", lat, lng]);
如何使回调起作用?
我刚刚知道如何让它发挥作用。
下面的代码使用 his/her ip 地址(html5 功能)跟踪用户的位置。它首先使用高精度 5 秒,然后在出现错误的情况下,尝试使用低精度 10 秒。
如果有错误,回调给出错误。如果地理定位有效,它会给出坐标。
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"]);
}
}
如何使用代码:在要跟踪用户的页面上,使用此 javascript
调用:
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
}
});
最初,我有一个有效的代码:
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"]);
}
}
然后我想整合这个页面的解决方案: http://jsfiddle.net/CvSW4/
现在我的代码看起来像这样,它不再工作了(在回调函数中丢失):
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"])
}
我收到错误:
Uncaught TypeError: object is not a functioncampaigns.min.js:37 successCallback
行是:
position(["coords", lat, lng]);
如何使回调起作用?
我刚刚知道如何让它发挥作用。
下面的代码使用 his/her ip 地址(html5 功能)跟踪用户的位置。它首先使用高精度 5 秒,然后在出现错误的情况下,尝试使用低精度 10 秒。
如果有错误,回调给出错误。如果地理定位有效,它会给出坐标。
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"]);
}
}
如何使用代码:在要跟踪用户的页面上,使用此 javascript
调用:
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
}
});