Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to know which HEMISPHERE? #32

Open
pierroo opened this issue Sep 29, 2023 · 4 comments
Open

how to know which HEMISPHERE? #32

pierroo opened this issue Sep 29, 2023 · 4 comments

Comments

@pierroo
Copy link

pierroo commented Sep 29, 2023

Considering how important this parameter is, is there any way to know which Hemisphere the user is from?
Perhaps it's out of scope for this library;
if we forward the iso country code, or the country (from react-native-localize for example), would it be possible to have a mapping function in this library to get the user's hemisphere?

Edit: I have found the following online:

window.whatHemisphere= (function(){
    var y= new Date().getFullYear();
    if(y.getTimezoneOffset()==undefined) return null;
    var jan= -(new Date(y, 0, 1, 0, 0, 0, 0).getTimezoneOffset()),
    jul= -(new Date(y, 6, 1, 0, 0, 0, 0).getTimezoneOffset()),
    diff= jan-jul;
    if(diff> 0) return 'N';
    if(diff< 0) return 'S'
    return null;
})()

do you think it could do the trick, and perhaps be part of your library?

@jasonsturges
Copy link
Owner

@pierroo Hi, I think that's trying to leverage Daylight Savings Time to deduce winter / summer - that might work in a lot of location, but I don't think is a reliable method.

Geolocation API is a better approach, but keep in mind some people block that data for security purposes.

But you'd be obtaining the latitude

  navigator.geolocation.getCurrentPosition((position) => {
    let lat = position.coords.latitude;
    let long = position.coords.longitude;
  });

I think positive latitude is Nothern Hemisphere, with negative latitude as Southern Hemisphere.

Interesting idea... I might incorporate that but keep in mind it requests permissions like sites that "want to use your current location". If a user blocks that, can't expect too much.

@pierroo
Copy link
Author

pierroo commented Sep 29, 2023

Otherwise if we don't want to rely on invasive API like the location one;
we can have a simple mapping of country VS north hemisphere or south
Country can be fetched directly from the device through react-native-localize.
as for the mapping, a json containing all countries and a boolean isNorth could do the job.
and obviously since it's nasty job to list and check them all, asking chatgpt to list it does the job pretty well:

image
(with a prompt before that was asking to make the output a json file etc)

@jasonsturges
Copy link
Owner

jasonsturges commented Sep 30, 2023

Ideal if an API provided this, but then probably a separate responsibility.

Targeting CJS browser capabilities, I'm unsure about React Native specific aspects in this library - I'd think the browser's locale would probably be telling via navigator.language.

Spent a lot of time in hell with Time Zone mappings with massive json datasets, which I'd prefer to avoid here.

This is 2k library, 1k gzip - which I had intended for this to be a micro package.

All good ideas, though - let me think about it.

@00ff88
Copy link

00ff88 commented Jul 21, 2024

I've had the same question a few days ago and found that currently, only using a third-party API like ipapi.co, you can achieve this with precise (though invasive) information. However, there are a few considerations to keep in mind: privacy, user consent, and terms of service. The easiest way might be to simply ask the user for their location, depending on the end use for your case.

It's important to note that the same time zones can exist in both hemispheres.

Using navigator.language or navigator.languages is also not a solution since a user can have multiple languages set from different hemispheres. Additionally, React Native determines locale based on the device settings, not its geographical position (see react-native-localize documentation).

Regarding the question, making a fetch request to https://ipapi.co/json/ will provide a response with the keys needed to determine the user's hemisphere.

fetch('https://ipapi.co/json/')
  .then(response => response.json())
  .then(data => {
    const latitude = data.latitude;
    const isNorth = latitude > 0;
    if (isNorth) {
      console.log("The user is in the Northern Hemisphere.");
    } else {
      console.log("The user is in the Southern Hemisphere.");
    }
  })
  .catch(error => {
    console.error("Error fetching the location:", error);
  });

Due to privacy concerns, I believe this functionality is out of scope for this package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants