如何在node.js中使用地理位置来获取用户设备经纬度 [英] How can I use geolocation in my node.js to get user device lat/long

查看:149
本文介绍了如何在node.js中使用地理位置来获取用户设备经纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,请原谅我的提问逻辑,因为我是新来的.我正在使用React和Node构建我的第一个全栈应用程序.我在想三种方法,但不确定哪种方法最有效.

Good day all, pardon my question logic as I am new here. I am building my first fullstack App using React and Node.I am thinking of three approaches but not sure what will work best.

一种方法

我希望能够在用户填写前端表单并请求通过geolocation API访问其geolocation时获得经纬度.例如,当用户提交其区域和社区名称时,后端将调用getCurrentPosition地理位置API.然后,返回的经/纬度将构成要发送到数据库的数据的一部分,如下面的提取代码所示.但是当我尝试这种方法时,我遇到了两个挑战.首先,错误消息未定义 navigator .其次,我不知道如何检索返回的经/纬度并将其声明为const以在创建位置时使用它.请参见下面的代码:

I want to be able to get user lat/long when they fill a form in the frontend and request access to their geolocation through the geolocation API. For example, when a user submit their region and community name, the backend will call the getCurrentPosition geolocation API. Then the returned lat/long will form part of the data to be sent to the database as shown in the extracted code below. But when I tried this approach, I ran into two challenges. First, an error message that navigator is not defined. Second, I don't know how to retrieved the lat/long returned and declare it as a const to use it in creating the location. See the code below:

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(success, error);
  } else {
    alert("Your browser is out of fashion. There is no geo location!")
  }

  function success(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude 
    console.log(`Your latitude is ${latitude} and your longitude is ${longitude}`)
      return (latitude, longitude);
  }

  function error() {
    alert("Can't detect your location. Try again later.")
  }

如何在函数外将lat/long声明为const.像这样:

How do I declare the lat/long as a const outside the function. Some like:

 const communityName = req.body.communityName;
 const latitude = (success).latitude;
 const longitude = (success).longitude;

APPROACH 2

我下载了geolocation npm软件包,并按以下代码使用它,但收到未定义导航器的错误.如果要使用这种方法,如何定义导航器,以及如何在函数外部将lat/long声明为const?

I downloaded the geolocation npm package and use it as in code below but get the error that navigator is not defined. If I am to use this approach, how can I define navigator and how can I get to declare the lat/long as a const outside of the function?

const geolocation = require('geolocation');

geolocation.getCurrentPosition(function (err, position) {
    if (err) throw err
    console.log(position)
  })

方法3

我了解了这种方法,但无法获得有效的示例.建议我应该使用地理位置API来获取用户设备经纬度,并将坐标作为req.body数据的一部分发送到后端API.如果这是一种好方法,那么我可以遵循一个教程吗?

I read about this approach but can't get a working example. It is suggested that I should use the geolocation API to get the user device lat/long and send the coords as part of req.body data to the backend API. If this is a good approach, is there a tutorial I can follow?

请问我是Java语言和编码的新手,但我已经取得了长足的进步,但这使我在项目中积stock了很多东西.没有答案是错误的租约.废话是有道理的.

Please I am new to Javascript and coding but I have made serious progress but this as got me stocked on my project. No answer is too wrong lease. There is sense in nonsense.

推荐答案

navigator.geolocation

navigator.geolocation

这是浏览器提供的API.它不适用于Node.js.

This is an API provided by browsers. It isn't available to Node.js.

我下载了地理位置npm软件包

I downloaded the geolocation npm package

这包装了 navigator.geolocation .它是为当您使用Node模块和Webpack提供的编译步骤编写客户端代码而设计的.它不适用于Node.js.

This wraps navigator.geolocation. It is designed for when you are writing client-code code using Node modules and a compilation step such as provided by Webpack. It doesn't work with Node.js.

建议我应该使用地理位置API来获取用户设备经纬度,并将坐标作为req.body数据的一部分发送到后端API.

It is suggested that I should use the geolocation API to get the user device lat/long and send the coords as part of req.body data to the backend API.

MDN

MDN and Google both provide introductions to the fetch API for making HTTP requests from client-side JavaScript.

React FAQ中还有一些特定于反应的信息.

There is also some React-specific information in the React FAQ.

然后您可以使用

You can then read the data in a different endpoint with req.body providing you have configured a suitable body-parsing middleware.

这篇关于如何在node.js中使用地理位置来获取用户设备经纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆