[SOLVED] Geolocation country

Hi!

Any way to get the country of the user? I’ve searched the internet, but couldn’t find a clear solution. I have to get the language of a user to show different text in an AR experience. Since Belgium en The Netherlands both speak Dutch “window.navigator.userLanguage” gives back “nl-nl” for both Belgium and The Netherlands, but we need english language for Belgium and Dutch for the Netherlands.

Thanks

Hi @DampedFred,

Keep in mind that geolocation based on IP is not terribly accurate because of the number of variables involved, but one of the easiest things you could do is just call IPify’s geolocation API and get the country from the response:

I hope this is helpful

1 Like

Looks promising! What code should I use in playcanvas to make the call?
Just regular javascript and pass through the value somehow to my other script that changes the language?

Hi @DampedFred,

This code is untested, and lacks an API key, so you may have to make some adjustments. Whenever you make your script that will make the call to IPify and make language decisions, it will look something like this:

var SomeScript = pc.createScript('someScript');

SomeScript.prototype.initialize = function() {

    this.country = null;    

    fetch('https://geo.ipify.org/api/v1?apiKey=YOUR_API_KEY')
        .then(response => response.json())
        .then(function (response) {
          this.country = response.location.country;
        }.bind(this));
};

On the previously posted link you can see what the response contains and pull any other information you might need from it. You will need to set up a free account at the very least to get an API key.

2 Likes

Awesome! Thanks a million for the effort! I’ll check it out.

1 Like

Hi
I guess the suggestion above is paid

its better to use ‘https://api.country.is/’ to get your location