Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6868977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:30:03+00:00 2026-05-27T03:30:03+00:00

I am using Node.js and MongoDB. Let say I have predefined few cities (e.g.

  • 0

I am using Node.js and MongoDB.
Let say I have predefined few cities (e.g. Seattle, Miami, New York) with Lat & Lon. and there is a user click to my website and I know his IP address, and find out the lat & lon. Then I want to know which city that I’ve defined is the closest to the user.

I know I can do it using Mongo’s geospatial feature. but it would be quite ‘expensive’ to use DB to calculate that for every web request.

Is there a Node.js NPM package that can do the geo feature as I described above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:30:04+00:00Added an answer on May 27, 2026 at 3:30 am

    If you already have the user’s location and the location of each city, it should be quite fast to compute the distance to the nearest. Check out this site: http://www.movable-type.co.uk/scripts/latlong.html
    I have used the first algorithm a few times.

      function getDistance(lat1,lat2,lon1,lon2){
        var R = 6371; // km
        var c = Math.PI / 180;
        var dLat = (lat2-lat1) * c;
        var dLon = (lon2-lon1) * c;
        var lat1 = lat1 * c;
        var lat2 = lat2 * c;
    
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        var d = R * c;
        return d;
       }
    var closest, dist = Number.MAX_VALUE;
    for(var i = 0, l=cities.length;i<l;++i){
      if(getDistance(cities[i].longitude, cities[i].latitude, user.longitude, user.latitude) < max){
      closest = cities[i];
     }
    }
    
      alert(closest.name + ' is the winner :)');
    

    You might want to add some exception handling here 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running node.js on OSX and have just installed mongodb using command npm install
I am new to Node, and I'm using Mongoose as a driver for MongoDB.
I have just installed MongoDB in my machine (Windows 7) and installed node-mongodb-native using
I have built my application using node.js, mongodb, redis, express, socket.io and planning to
I have a Node.js app using Express that stores data in a mongoDB (locally).
Anyone know how to get the position of a node using XPath? Say I
I have the following XPATH line: //det[@nItem=1]/prod/cProd That successfully selects the desired node using
js.I am trying to create a file upload using node.js and mongodb.I am getting
I have recently gone into extending my site using node.js and have come to
I'm using MongoDB with Node.js. Is there any speed advantage to using a MapReduce

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.