How to implement geoinformation in node.js and mongodb

I saw several examples of using geoip in node.js such as https://github.com/kuno/GeoIP.git and https://github.com/wadey/node-geoip . However, I want to display a map showing geoip for a specific user in the log. How can it be implemented.

+5
source share
5 answers

You can get the geolocation database (for example, http://www.maxmind.com ) and save it in mongo. Each entry contains an IP range (start / end) and latitude / longitude associated with that IP range. IP addresses are represented as integers. You can create an index in the field startand execute a query on mongo to find the record with the highest value startthat is less than the IP address of your client and see the corresponding lat / lon.

Regarding building a map using this lat / lon, it’s very easy to create a google map that is concentrated in a specific place: (View source at: http://code.google.com/apis/maps/documentation/javascript/v2/examples /map-simple.html )

/ , , . , .

+8

GeoIP https://github.com/kuno/GeoIP, ( , ), Maxmind, , .

MongoDB, . Node.js.

:

// Open the GeoLiteCity.dat file first.
var City = geoip.City;
var city = new City('/path/to/GeoLiteCity.dat');
console.log(city); // this contains country, city, lat, long, continent, postal code etc
+4

Python:

#!/usr/bin/python
#coding: utf-8
import os
import pygeoip


gi = pygeoip.GeoIP('GeoIP.dat')
gic = pygeoip.GeoIP('GeoIPCity.dat')


fl = file(r'apache-unique.log')
lines = fl.readlines()

for line in lines:
    print gi.country_code_by_addr(line)
    print gic.record_by_addr(line)

os.system('pause')
0

NPM API IPLocate.io, , (, ) IP-.

, 1500 .

npm install node-iplocate

const iplocate = require("node-iplocate");

iplocate("8.8.8.8").then(function(results) {
  console.log("IP Address: " + results.ip);
  // IP Address: 8.8.8.8
  console.log("Country: " + results.country + " (" + results.country_code + ")"); 
  // Country: United States (US)
  console.log("Continent: " + results.continent); 
  // Continent: North America
  console.log("Organisation: " + results.org + " (" + results.asn + ")"); 
  // Organisation: Google LLC (AS15169)

  console.log(JSON.stringify(results, null, 2));
  /*
    {
      "ip": "8.8.8.8",
      "country": "United States",
      "country_code": "US",
      "city": null,
      "continent": "North America",
      "latitude": 37.751,
      "longitude": -97.822,
      "time_zone": null,
      "postal_code": null,
      "org": "Google LLC",
      "asn": "AS15169"
    }
  */
});

// Or with callbacks
iplocate("8.8.8.8", null, function(err, results) {
  // ...
  console.log(JSON.stringify(results, null, 2));
});

// Provide an API key from IPLocate.io
iplocate("8.8.8.8", { api_key: "abcdef" }).then(function(results) {
  // ...
});
0

mpobrien -

.

db maxmind

http://dev.maxmind.com/geoip/geoip2/geolite2/ http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz

node -maxmind-db

https://github.com/PaddeK/node-maxmind-db

var mmdbreader = require('maxmind-db-reader');

// open database
var countries = mmdbreader.openSync('./countries.mmdb');

// get geodata
app.get('/api/v1/ip/', function(req, res) {
    var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;

    countries.getGeoData(ip, function(err, geodata) {
        if(!err && geodata.location) return res.json({success: true, location: geodata.location});
        return res.json({success: false, location: null, error: err});
    });
});

IP-, .

-

. db . . .. mmdbreader.openSync()

-1

All Articles