首页 > 解决方案 > 地理位置真的很慢我做错了什么?

问题描述

我尝试从浏览器获取位置,但从 5 秒到 8 秒真的很慢。不知道为什么这么慢

const getUserGeolocation = (): Promise<{latitude: number, longitude: number}> => new Promise(async (resolve) => {
    navigator.geolocation.getCurrentPosition(async pos => {
        resolve({latitude: pos.coords.latitude, longitude: pos.coords.longitude})
    })
})
const {latitude, longitude} = await getUserGeolocation()

标签: javascripttypescriptecmascript-6geolocation

解决方案


可能只是因为使用了太多async。从代码中删除两个 async 关键字。我认为这将帮助您更快地执行代码。


推荐阅读