首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >地理定位出口lat和lng

地理定位出口lat和lng
EN

Stack Overflow用户
提问于 2018-07-19 10:16:00
回答 1查看 175关注 0票数 0

我使用两个函数,第一个是使用地理定位API,我想返回lat和lng。

在第二个函数中,我想用这个坐标获取一些数据,但是我不能在第一个函数上正确地导出它。

我得到geolocationData()不是一个函数。

这是我的密码

代码语言:javascript
运行
复制
const geolocationData = () => {
    return navigator.geolocation.getCurrentPosition((position) => {
        return position
    }, () => {
        alert('Unable to fetch your location')
    }, { enableHighAccuracy: true })
}

const gpsLocation = async () => {

    const lat = geolocationData().position.coords.latitude
    const lng = geolocationData().position.coords.longitude

    const address = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&result_type=administrative_area_level_4&key=0000000000000000`)
    const weatherData = await fetch(`https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/0000000000000/${lat},${lng}?units=si&extend=hourly&exclude=flags&lang=el`)

    return {
        address: address.json(),
        weatherData: weatherData.json()
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-19 12:09:28

这是因为getCurrentPosition的工作方式与您预期的不同,因为它不直接返回latlong

让我重构一下这段代码。

我将通过创建一个用当前地理坐标解析的承诺来实现这个承诺,并在您的主gpsLocation函数中调用这个承诺。由于您使用的是async/await,所以我也会这样保存它。总的来说,它应该是这样的:

代码语言:javascript
运行
复制
//  a promise that resolves with geo coordinates
const getPosition = (options) => {
  return new Promise(function (resolve, reject) {
    navigator.geolocation.getCurrentPosition(resolve, reject, options);
  });
}

const gpsLocation = async () => {

    try {

        // calling the promise and awaiting the position object
        const geolocationData = await getPosition({ enableHighAccuracy: true });

        // destructruing the coordinates from the object
        const {latitude: lat, longitude: lng} = geolocationData.coords;

        // creating promises for each api call
        const addressPromise = fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&result_type=administrative_area_level_4&key=0000000000000000`)
        const weatherDataPromise = fetch(`https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/0000000000000/${lat},${lng}?units=si&extend=hourly&exclude=flags&lang=el`);

        // wating for the promises to be resolved in parallel (rather than one after another)
        const [address, weatherData] = await Promise.all([addressPromise, weatherDataPromise]);

        return {
            address: address.json(),
            weatherData: weatherData.json()
        }

    } catch(e) {
        alert('Unable to fetch your location')
    }


}

下面是如何使用它:

代码语言:javascript
运行
复制
(async () => {

    const { address, weather } = await gpsLocation();

    console.log(address);
    console.log(weather);

})();

(让我知道以上为你所做的工作;)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51420037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档