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 6950217
In Process

The Archive Base Latest Questions

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

I would like to use client-side JavaScript to find the centroid of a set

  • 0

I would like to use client-side JavaScript to find the centroid of a set of latitudes + longitudes (actually Google LatLng objects), using a simple mean calculation.

I see that similar questions has been asked many times before on Stack Overflow, but I can’t find a straightforward answer for JavaScript. (This may just be a fail of my Googling, apologies if this is a duplicate.)

I have something like this, but it doesn’t work for the case where you’re averaging, say, latitudes of 179 and -179, and so the centroid should be 180 rather than 0.

var avg_lat, avg_lng;
for (var i = 0; i < google_latlngs.length; i++) { 
    avg_lat += google_latlngs[0].lat();
    avg_lng += google_latlngs[1].lng();
}
avg_lat = avg_lat / google_latlngs.length; 
avg_lng = avg_lng / google_latlngs.length; 

I need to do this efficiently in client-side JavaScript, and my points are unlikely to be more than a few km apart, so great-circle distance or anything fancy really isn’t necessary in this case.

Thanks for your help.

UPDATE: OK, any method for finding a centroid in JavaScript will do.

  • 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-27T14:03:55+00:00Added an answer on May 27, 2026 at 2:03 pm

    If you are dealing with 2 points only, make sure the difference between your two latitude points is less than or equal to 180 before applying your function. You can do this by adding or subtracting by 360, which would change -179 to 181 (or 179 to -181). When you get your final result, add/subtract by 360 until the final value is within your desired range.

    Update

    If you want this to work with more than two points, we’ll have to do some geometry. We treat each latitude point as a point on a unit circle with a certain distance x and y from the origin and certain angle a (the latitude):

    Latitude on a unit circle

    We must average all the x‘s and y‘s and then take the angle of the resulting point to get our final latitude for the centroid. Here is the JavaScript:

    var latXTotal = 0;
    var latYTotal = 0;
    var lonDegreesTotal = 0;
    
    var currentLatLong;
    for (var i = 0; currentLatLong = google_latlngs[i]; i++) { 
        var latDegrees = currentLatLong.lat();
        var lonDegrees = currentLatLong.lng();
    
        var latRadians = Math.PI * latDegrees / 180;
        latXTotal += Math.cos(latRadians);
        latYTotal += Math.sin(latRadians);
    
        lonDegreesTotal += lonDegrees;
    }
    
    var finalLatRadians = Math.atan2(latYTotal, latXTotal);
    var finalLatDegrees = finalLatRadians * 180 / Math.PI;
    
    var finalLonDegrees = lonDegreesTotal / google_latlngs.length;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use client-side Javascript to perform a DNS lookup (hostname to
I would like to use mongodb directly from the client-side, using javascript inside my
Can anyone help me. I don't use Client-side Javascript often with HTML. I would
I would like to have images, css, and javascript cached client-side on their browser
I have a javascript array of objects that I would like to use to
I would like to use javascript to develop general-purpose GUI applications. Initially these are
I am creating a simple form. I would like to use embedded javascript to
I would like to use the CommonJS module system in a clientside javascript application.
I'm trying to configure a web application that can use client-side JavaScript for localization
I am using AdoNetAppender (SQL server) in my asp.net application and would like use

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.