Michel
Michel

Reputation: 11755

How to use GPS with Node.js?

I have a Node.js app where I would like to use the GPS information of the user. I have never done that before in a web page or using javascript.

A little research on the net led me here: https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation_error

That seems to show that what I want to do is basically possible.

Nevertheless when I try to do something similar, I get this error:

ReferenceError: navigator is not defined

The relevant code is:

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

Obviously "navigator" is unknown whith Node.js.

Is there some good and simple example or tutorial showing how I can use the GPS within a Node.js app?

Upvotes: 1

Views: 2943

Answers (1)

Rob M.
Rob M.

Reputation: 36521

You could sent the data from the client using the geolocation API or use GeoIP via node-geoip or node-geoip-native and lookup their coordinates based on IP address. Here's an example implementation from 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 }

Upvotes: 2

Related Questions