JavaScript:Can';t从另一个函数获取变量的值

JavaScript:Can';t从另一个函数获取变量的值,javascript,Javascript,我面临着从另一个函数获取变量值的问题。我试图从谷歌地图中的当前位置获得该地点的距离,但使用了计算距离的哈弗森公式 我的HTML: <p> <script> lat = "<?php echo $atm_row_data->latitude;?>"; lng = "<?php echo $atm_row_data->longitude;?>"; dist = getDistance(lat, lng); documen

我面临着从另一个函数获取变量值的问题。我试图从谷歌地图中的当前位置获得该地点的距离,但使用了计算距离的
哈弗森公式

我的HTML:

<p>
  <script>
  lat = "<?php echo $atm_row_data->latitude;?>";
  lng = "<?php echo $atm_row_data->longitude;?>";
  dist = getDistance(lat, lng);
  document.write(dist);
  </script>
</p>

lat=“”;
液化天然气=”;
距离=距离(纬度,液化天然气);
文件编写(dist);

我的JavaScript:

var curPosition;
var lat, lng;

/**** get current position ****/
function getPosition() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showCurrentPosition);
    } else {
        alert('Geolocation is not supported by this browser.');
    }
}

function showCurrentPosition(position) {
    lat = position.coords.latitude;
    lng = position.coords.longitude;
    curPosition = new google.maps.LatLng(lat, lng);
    console.log('curPosition: '+curPosition); <--- this works
}

/******** Haversine Formula of Getting Distance ********/
var rad = function(x) {
  return x * Math.PI / 180;
};

function getDistance(lt, lg) {
    console.log(curPosition); <--- this shows undefined
    var p1 = curPosition;
    var R = 6378137; // Earth’s mean radius in meter
    var dLat = rad(lt - p1.lat());
    var dLong = rad(lg - p1.lng());
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(rad(p1.lat())) * Math.cos(rad(lt)) *
    Math.sin(dLong / 2) * Math.sin(dLong / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = R * c;
    if (d >= 1000)
        return Math.round( (d / 1000) * 100) / 100 + "Km";
    else
        return Math.round( (d * 100) / 100) + "m";
};
/******** END Haversine Formula of Getting Distance ********/
var-curPosition;
var lat,液化天然气;
/****获取当前位置****/
函数getPosition(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showCurrentPosition);
}否则{
警报('此浏览器不支持地理位置');
}
}
功能显示当前位置(位置){
纬度=位置坐标纬度;
lng=位置坐标经度;
curPosition=new google.maps.LatLng(lat,lng);

console.log('curPosition:'+curPosition);在脚本标记中,您只调用
getDistance(lat,lng)
,但从未调用
showCurrentPosition(position)
!因此变量
curPosition
未定义,因为它尚未定义

您需要调用变量
curPosition
来保存值

也许在
getDistance()
函数的开头调用
getPosition()
可以解决这个问题,就像调用
showCurrentPosition
一样


用HTML显示位置的替代方法(这只是一个快速片段,您可以根据自己的喜好对其进行调整):

它与其他函数的功能基本相同,但它依赖于单响应性原则,因此一旦调用此函数,您就可以随心所欲地操作
curPosition

了解google api,navigator.geolocation有一个回调函数,所以showCurrentposition是回调函数,但事情从来都不是这样呼叫
getPosition()检查控制台。是否有错误?是否没有得到正确答案?什么都没有?我猜您从未调用
showCurrentPosition
,因此
curPosition
仍然未定义。问题很可能是变量作用域。您正试图访问作用域之外的变量。请尝试在b中定义
curPosition
正在任何函数外部登录。控制台抛出:未定义的未捕获类型错误:无法读取的属性“lat”undefined@h4kl0rd但是在调用
getDistance()
之前没有调用
getPosition()
。我在
initialize()
函数中调用了
getPosition()
,并尝试在
getDistance()之前调用它
但仍然抛出未定义。我只想在html中显示距离。可能还有另一种方法。我尝试进行编辑,但仍然返回未定义。仅当我将
return curPosition
放在
navigator.geolocation…
中时,它才会起作用,然后在函数外部或从另一个位置调用时返回未定义她的函数。@h4kl0rd那么您有两个选择:要么在该函数中修改UI(
getPositionHTML
),或者您使用Promise对象来确保
getCurrentPosition
功能已成功完成,然后使用
然后
来进行UI修改。我们可以开始聊天吗?我更新了我的答案,笔会告诉您从当前位置到阿姆斯特丹的距离:D,复制源代码以查看如何完成,我添加了t他正确的公式,与他们的家伙谁做了所有的数学计算(克里斯Vesies)的链接,我的代码显示你如何计算从点到点的位置,我没有复制你所有的代码到它,只有需要的代码,我命名的功能在地图{}如果这有帮助,请接受为“答案”谢谢你的笔,我看了一遍,但它只适用于一个位置,而不是多个位置。我在许多位置循环,并将他们的LAT和LONG传递到
getDistance()
获取从我的当前位置到这些位置的距离。@h4kl0rd,那么你应该将当前位置和你希望计算到的位置从你的循环传递到getdistance函数,这样你就可以获取所有位置的距离。只需将它添加到你的循环中,并存储在某个地方。-如果它对一个位置有效,它对manyi Didha也有效我有时间调试您的代码,但是通过快速查看,我认为这在succes中是未定义的,在回调中是未定义的,因此它的作用域不同,所以第72行:navigator.geolocation.getCurrentPosition(this.success,this.fail);应该是navigator.geolocation.getCurrentPosition(this.success.bind(this),this.fail.bind(this));以使此成功具有相同的范围。
function getPositionHTML() {
    navigator.geolocation.getCurrentPosition(function(pos) {
        curPosition = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
    });
    return curPosition;
}
function getDistance(lt, lg) {
    console.log(curPosition); <--- this shows undefined <---- so this will always be undefined
    var p1 = curPosition;
    var R = 6378137; // Earth’s mean radius in meter
    var dLat = rad(lt - p1.lat());
    var dLong = rad(lg - p1.lng());
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(rad(p1.lat())) * Math.cos(rad(lt)) *  <--- see comment
    Math.sin(dLong / 2) * Math.sin(dLong / 2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = R * c;
    if (d >= 1000)
        return Math.round( (d / 1000) * 100) / 100 + "Km";
    else
        return Math.round( (d * 100) / 100) + "m";
};