我正在尝试以将纬度和经度变量传递给$_POST
变量的形式创建隐藏的输入字段,但不断返回“未定义”。
我正在使用navigator.geolocation.getCurrentPostition
方法。
任何人都知道为什么变量locationLat
和locationLon
会返回undefined?
$(document).ready (function () {
var locationMessage, locationLat, locationLon;
var inputLat = document.createElement("input");
inputLat.setAttribute("type", "hidden");
inputLat.setAttribute("name", "inputLat");
inputLat.setAttribute("id", "inputLatitude");
var inputLon = document.createElement("input");
inputLon.setAttribute("type", "hidden");
inputLon.setAttribute("name", "inputLon");
inputLon.setAttribute("id", "inputLongitude");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition (
function (position) {
// set value for location data
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
locationLat = latitude;
locationLon = longitude;
inputLat.setAttribute("value", locationLat);
inputLon.setAttribute("value", locationLon);
},
function (error) {
var errorTypes = {
0: "Unknown error",
1: "Permission denied by user",
2: "Position is not available",
3: "Request timed out"
};
var errorMessage = errorTypes[error.code];
if (error.code == 0 || error.code == 2) {
errorMessage += (": " + error.message);
}
locationMessage = errorMessage;
// if error set value for location data
inputLat.setAttribute("value", locationMessage);
inputLon.setAttribute("value", locationMessage);
});
}
document.getElementById("series").appendChild(inputLat);
document.getElementById("series").appendChild(inputLon);
});