【问题标题】:Is it possible to find device geo location based on ip address using NodeJS?是否可以使用 NodeJS 根据 IP 地址查找设备地理位置?
【发布时间】:2022-01-14 07:08:58
【问题描述】:

我正在尝试实现一项功能,例如 Instagram 提供的功能;我说的是它的“登录活动”页面。这就是我所说的:

每次用户登录时,当前正在使用的设备都会存储在页面中。我该怎么办?

我的当前代码使用了两个库,一个用于从正在使用的设备中查找 ip.address(),另一个用于帮助我获取有关给定 IP 地址的位置数据。可悲的是,我无法让它工作。代码是这样的:

const ipLocation = require('ip-to-location')
const ip = require('ip')

const loc = await ipLocation.fetch(ip.address())
console.dir('Location', loc)
await User.findOneAndUpdate(
  { email: req.body.email },
  {
    $push: {
      loginActivity: {
        type: 'Point',
        coordinates: [loc.longitude, loc.latitude],
        // formattedAddress: loc[0].formattedAddress,
        // street: loc[0].streetName,
        city: loc.city.city,
        state: loc.region_name,
        zipcode: loc.zip_code,
        country: loc.country_code,
      },
    },
  }
)

是否有任何替代库可供使用?或者有人可以指出我正确的方向以使这成为可能吗?

【问题讨论】:

    标签: javascript node.js mongodb geolocation ip-geolocation


    【解决方案1】:

    嗯,如果你能得到一个用户的ip地址,你可以试试ipgeolocation.io

    这是一个免费的 API,可以为您提供 IP 地址的纬度和经度值(如果您需要,还可以提供更多信息)。

    更多documentation

    【讨论】:

    • 嘿,很抱歉再次回到这个问题。你以前用过吗?你知道如何返回值并将它们存储在变量中吗?我试过了,但还是不行:Link to Snippet
    • 好吧,我从来没有尝试过node js模块。相反,我首先创建了我的服务器,然后我的服务器使用我的 api 密钥和目标 IP 地址向 ipgeolocation.io 发送了一个获取请求,然后通过我的应用程序传递它。你可以按照同样的方法。文档:ipgeolocation.io/documentation/ip-geolocation-api.html
    【解决方案2】:

    你可以试试 IP2Location Node.js。

    https://github.com/ip2location/ip2location-nodejs

    它可以调用数据库文件(freepaid)或调用web service

    对于数据库,代码如下所示:

    const {IP2Location} = require("ip2location-nodejs");
    
    let ip2location = new IP2Location();
    
    ip2location.open("./DB25.BIN");
    
    testip = ['8.8.8.8', '2404:6800:4001:c01::67'];
    
    for (var x = 0; x < testip.length; x++) {
        result = ip2location.getAll(testip[x]);
        for (var key in result) {
            console.log(key + ": " + result[key]);
        }
        console.log("--------------------------------------------------------------");
    }
    
    ip2location.close();
    

    对于网络服务,您可以使用以下示例:

    const {IP2LocationWebService} = require("ip2location-nodejs");
    
    let ws = new IP2LocationWebService();
    
    let ip = "8.8.8.8";
    let apiKey = "YOUR_API_KEY";
    let apiPackage = "WS25";
    let useSSL = true;
    
    // addon and lang to get more data and translation (leave both blank if you don't need them)
    let addon = "continent,country,region,city,geotargeting,country_groupings,time_zone_info";
    let lang = "fr";
    
    ws.open(apiKey, apiPackage, useSSL);
    
    ws.lookup(ip, addon, lang, (err, data) => {
        if (!err) {
            console.log(data);
            
            ws.getCredit((err, data) => {
                if (!err) {
                    console.log(data);
                }
            });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2018-01-05
      • 1970-01-01
      • 2010-09-07
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 2018-05-19
      相关资源
      最近更新 更多