gpt4 book ai didi

javascript - 如何在 Node.js 中使用 GPS?

转载 作者:行者123 更新时间:2023-11-30 15:03:38 27 4
gpt4 key购买 nike

我有一个 Node.js 应用程序,我想在其中使用用户的 GPS 信息。我以前从未在网页或使用 javascript 中这样做过。

网上的一点研究让我来到这里: https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation_error

这似乎说明我想做的事情基本上是可以的。

然而,当我尝试做类似的事情时,我得到了这个错误:

ReferenceError: navigator is not defined

相关代码为:

if (navigator.geolocation) {
// Do something useful with the location.
}

显然“导航器”在 Node.js 中是未知的。

是否有一些好的简单示例或教程展示了我如何在 Node.js 应用程序中使用 GPS?

最佳答案

您可以使用地理位置 API 从客户端发送数据或通过 node-geoip 使用 GeoIP或 node-geoip-native并根据 IP 地址查找他们的坐标。这是来自 node-geoip

的示例实现
var geoip = require('geoip-lite');

var ip = "207.97.227.239";
var geo = geoip.lookup(ip);

console.log(geo);
{ range: [ 3479297920, 3479301339 ],
country: 'US',
region: 'TX',
city: 'San Antonio',
ll: [ 29.4889, -98.3987 ],
metro: 641,
zip: 78218 }

关于javascript - 如何在 Node.js 中使用 GPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46126938/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com