"currentLoc"を返したいと思います。残念ながら、それは私のためには機能しません。 コールバック関数の結果を割り当てて、コード全体で再利用することは可能ですか?成功コールバック関数の結果の値を変数に代入する方法
function currentLocation() {
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
currentLoc = new google.maps.LatLng(crd.latitude, crd.longitude);
};
function error(err) {
///ERROR
};
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(success, error, options);
return currentLoc;
}
'success'は非同期です。つまり、' currentLocation'関数から返された結果は 'success'が実行される前に発生する可能性があります。すべてが「成功」で起こらなければならない。また、 'navigator.geolocation.getCurrentPosition'は、呼び出されるたびにその場所にアクセスする権限をユーザーに求めますが、' navigator.geolocation.watchPostion'は一度だけ尋ねます。 – PHPglue