【问题标题】:Get geolocation coordinates [ latitude, longitude ] of current location in NextJS/ReactJS [closed]在 NextJS 中获取当前位置地址 [关闭]
【发布时间】:2021-12-17 13:36:14
【问题描述】:

我想在 nextjs 上做项目,我想获取当前用户位置地址。但在下一个 js 导航窗口中不提供值。那么在 NextJS 中该怎么做呢?

【问题讨论】:

  • 你要的是当前路径吗?
  • 当前地址位置
  • 你这是什么意思?您能否向我们展示一些代码以更好地解释您要实现的目标?

标签: reactjs next.js currentlocation userlocation


【解决方案1】:

使用router.pathName - 这提供了当前路径 -

import { useRouter } from 'next/router'

const router = useRouter();

const currentPath = router.pathName

//...rest of your code

这确保了通过 SSR 和 CSR 的可用性

参考:https://nextjs.org/docs/api-reference/next/router

【讨论】:

    【解决方案2】:

    我通过以下方式解决它:-

    useEffect(()=> {
        navigator.geolocation.getCurrentPosition(function(position) {
          console.log("Latitude is :", position.coords.latitude);
          console.log("Longitude is :", position.coords.longitude);
        });
      })
    

    【讨论】:

    • 工作正常!谢谢老兄。
    猜你喜欢
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多