在Javascript中访问变量
我有以下代码:在Javascript中访问变量,javascript,geolocation,Javascript,Geolocation,我有以下代码: function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { alert('Geolocation is not supported by this browser'); } } function showPosition(position
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
else {
alert('Geolocation is not supported by this browser');
}
}
function showPosition(position) {
alert(position.coords.latitude + ' ' + position.coords.longitude);
var Loc = position.coords.latitude + ' ' + position.coords.longitude;
}
我需要能够在函数外部或通过GetLocation函数访问Loc变量。因为我需要将它绑定到一个网格中,我正在创建一个网格,用于存储非现场员工上班和下班的时钟信息
下面是使用变量Loc的代码
getLocation();
var rowId=timesheetGrid.uid();
var pos = timesheetGrid.getRowsNum();
timesheetGrid.addRow(rowId,["","","","<?php echo $ActiveWeek; ?>","","","","","","","","","","",Loc],0); //adds a new row with pre filled data
timesheetGrid.selectCell(rowId,0,false,true,true); // Focus on the new row
getLocation();
var rowId=timesheetGrid.uid();
var pos=timesheetGrid.getRowsNum();
timesheetGrid.addRow(行ID,[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Loc],0)//添加带有预填充数据的新行
timesheetGrid.selectCell(rowId,0,false,true,true);//关注新行
任何可以通过两种方式获得的帮助都将不胜感激
- 在函数外部声明Loc
var Loc = ''; function showPosition(position) { alert(position.coords.latitude + ' ' + position.coords.longitude); Loc = position.coords.latitude + ' ' + position.coords.longitude; }
- 或者,只需删除Loc中的“var”。它现在被声明为全局()
- 在函数外部声明Loc
var Loc = ''; function showPosition(position) { alert(position.coords.latitude + ' ' + position.coords.longitude); Loc = position.coords.latitude + ' ' + position.coords.longitude; }
- 或者,只需删除Loc中的“var”。它现在被声明为全局()
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(whenWeGetPosition);
}
else {
alert('Geolocation is not supported by this browser');
}
}
function whenWeGetPosition(position) {
// your code to use position
var rowId=timesheetGrid.uid();
var pos = timesheetGrid.getRowsNum();
timesheetGrid.addRow(rowId,["","","","<?php echo $ActiveWeek; ?>","","","","","","","","","","",Loc],0); //adds a new row with pre filled data
timesheetGrid.selectCell(rowId,0,false,true,true); // Focus on the new row
}
getLocation();
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(whenWeGetPosition);
}
否则{
警报(“此浏览器不支持地理位置”);
}
}
换位时的功能(位置){
//你的代码使用位置
var rowId=timesheetGrid.uid();
var pos=timesheetGrid.getRowsNum();
timesheetGrid.addRow(rowId、[“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、Loc),0);//添加一
timesheetGrid.selectCell(rowId,0,false,true,true);//关注新行
}
getLocation();
navigator.geolocation.getCurrentPosition()正在异步模式下工作,函数showPosition(position)是用于捕获位置的回调函数。所以您需要将所有与位置相关的代码放在这个回调函数中。例如,您可以这样做:
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(whenWeGetPosition);
}
else {
alert('Geolocation is not supported by this browser');
}
}
function whenWeGetPosition(position) {
// your code to use position
var rowId=timesheetGrid.uid();
var pos = timesheetGrid.getRowsNum();
timesheetGrid.addRow(rowId,["","","","<?php echo $ActiveWeek; ?>","","","","","","","","","","",Loc],0); //adds a new row with pre filled data
timesheetGrid.selectCell(rowId,0,false,true,true); // Focus on the new row
}
getLocation();
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(whenWeGetPosition);
}
否则{
警报(“此浏览器不支持地理位置”);
}
}
换位时的功能(位置){
//你的代码使用位置
var rowId=timesheetGrid.uid();
var pos=timesheetGrid.getRowsNum();
timesheetGrid.addRow(rowId、[“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、“”、Loc),0);//添加一
timesheetGrid.selectCell(rowId,0,false,true,true);//关注新行
}
getLocation();
为什么不返回该值?您设置了它,但不使用它做任何事情,这是毫无意义的。您从何处调用此方法?在函数范围之外声明它,从内部分配值。或者从函数返回它:
函数showPosition(position){…;return Loc}
。顺便说一句,按照惯例,以大写字母开头的变量是为构造函数保留的。PHP与此有什么关系?为什么要添加PHP标记?为什么不直接返回该值?您设置了它,但不使用它做任何事情,这是毫无意义的。您从何处调用此方法?在函数范围之外声明它,从内部分配值。或者从函数返回它:函数showPosition(position){…;return Loc}
。顺便说一句,按照惯例,以大写字母开头的变量是为构造函数保留的。PHP与此有什么关系?为什么要添加PHP标记?然后如何在代码的第二部分中显示它?当用户单击添加行按钮timesheetGrid.addRow(行ID,[,,,,,,,“2016-03-09”,“;如果我理解了你的要求,我想你就必须解析它。那么我如何在代码的第二部分中显示它呢?当用户单击添加行按钮timesheetGrid.addRow(行ID,[,,,,,,,“2016-03-09”,“;如果我理解了你的要求,我想你必须解析它。