简体   繁体   English

HTML5地理位置不等待用户输入

[英]HTML5 Geolocation Not Waiting for user Input

I am trying to create a web app that will prompt the user to allow the use of his location. 我正在尝试创建一个Web应用程序,它将提示用户允许使用他的位置。 I am using the following function. 我正在使用以下功能。

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            function(position){
                return("@" + position.coords.latitude + "," + position.coords.longitude);
            }
        );
    } else {
        return("Unknown");
    }
}

My issue is that the function returns "undefined" before the user is even prompted to allow for location tracking. 我的问题是,在提示用户进行位置跟踪之前,该函数返回“undefined”。 Does anyone know how I could modify this code to wait until after the user chooses? 有谁知道如何修改此代码,等到用户选择后? Also I can't seem to see what is throwing the undefined error. 此外,我似乎无法看到什么是抛出未定义的错误。

Promises is a simple answer for this kind of issue. 对于这类问题, 承诺是一个简单的答案。 navigator.geolocation.getCurrentPosition is an asynchronous call - that means that you call the method and pass it a callback function, whenever the location is retrieved then your callback function will be called with the result value (location). navigator.geolocation.getCurrentPosition是一个异步调用 - 这意味着您调用该方法并向其传递回调函数,每当检索到该位置时,将使用结果值(位置)调用您的回调函数。 Depending on the asynchronous function you're calling, it could take 1ms for your callback function to be called or 1 hour, all you know is that whenever the value is available, it will "notify" you. 根据您正在调用的异步函数,可能需要1ms才能调用您的回调函数或1小时,所有您知道的是,只要值可用,它就会“通知”您。

In the case of your getLocation function, you either return nothing at all or you return the string Unknown . 对于getLocation函数,您要么根本不返回任何内容,要么返回字符串Unknown Notice that returning a result from the callback function(position){ has nothing to do with returning on getLocation . 请注意,从回调function(position){返回结果function(position){与返回getLocation无关。 A very simple solution (without using Promises) is to simply give a callback to your getLocation function, so it would look like something like this: 一个非常简单的解决方案(不使用Promises)就是简单地给你的getLocation函数提供一个回调,所以它看起来像这样:

function getLocation(callback) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            function(position){
                callback("@" + position.coords.latitude + "," + position.coords.longitude)
            }
        );
    } else {
      return "Unknown";
    }
}

getLocation(function(res) { /* do something with the result */ });

As I mentioned before, another way of fixing this is to use Promises . 正如我之前提到的,另一种解决方法是使用Promises Either use native ES6 promises or go for a widely used library like Bluebird (my favourite). 要么使用本机ES6承诺,要么使用像Bluebird (我最喜欢的)这样广泛使用的库。 With promises, the code would look something like this: (I recommend you to have a read about it since Javascript is all about asynchronous methods, so you should have a good grasp on these kind of issues) 使用promises,代码看起来像这样:(我建议你读一下它,因为Javascript是关于异步方法的,所以你应该很好地掌握这些问题)

function getLocation(callback) {
    var promise = new Promise(function(resolve, reject) {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(
                function(position){
                    resolve("@" + position.coords.latitude + "," + position.coords.longitude)
                }
            );
        } else {
          reject("Unknown");
        }
    });

    return promise;
}

var locationPromise = getLocation();
locationPromise
      .then(function(loc) { console.log(loc); })
      .catch(function(err) { console.log("No location"); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM