How can I get the country and region code using geolocation instead of full country and region names

10,108

Solution 1

You should use the short_name value for the country, which is the two-digit country code. These are ISO standard codes and should be the basis for your database lookup, not the localized country name.

Solution 2

Another options, which has ISO codes for administrative regions, is MaxMind's GeoIP2 JavaScript API:

http://dev.maxmind.com/geoip/geoip2/javascript/

It uses W3C geolocation when available and falls back on MaxMind's IP geolocation database otherwise. The free version requires attribution somewhere on your site.

Share:
10,108
Julien Spronck
Author by

Julien Spronck

Hi, I am a Senior Software Engineer at the Minerva Project working with a broad range of technologies (Python/Django, React/Redux, Terraform/Kubernetes, ...). Before this, I was an instructor for a Data Analytics and Visualization bootcamp at UC Berkeley Extension where students work their way through the industry’s most popular programming languages, tools, and libraries in data analytics (including but not limited to, Microsoft Excel, Python, JavaScript, HTML/CSS, SQL, Hadoop, Tableau, Advanced Statistics, Machine Learning, R, Git/GitHub). Previously, I was a researcher at Leiden University, working as project manager and lead scientist on the Multi-site All-Sky CAmeRA project (MASCARA). MASCARA consists of several fully-robotic stations distributed across the globe with the goal to find planets orbiting around the brightest stars. I obtained my Ph.D. in applied optics from Delft University of Technology. I then worked at San Francisco State University and then Yale University as a Postdoctoral Researcher and later on, as a Research Scientist. During that time, I designed, integrated and tested interferometers, high-resolution spectrographs, optical fiber scramblers and various imaging systems. My contributions have spanned many aspects of instrumentation and optics in general. My work was published in refereed papers, book chapters and proceedings, and presented at many international conferences. In 2007, I published an international patent entitled “Polarization nulling interferometry”. Throughout the years, I became proficient in optical and mechanical CAD software, such as Zemax, SolidWorks or AutoDesk Inventor. In addition, I have gained valuable experience in project management. Over the years, I also have developed strong programming skills. In particular, I have worked on data analysis and numerical simulations (Matlab, IDL, Python, LabView). I am proficient in Python: for example, I developed the control and data-acquisition software for completely robotic operations in the MASCARA project. I also particularly enjoy web design (html, html5, php, css, javascript, jQuery, mysql, …), which I used for both professional and personal projects. I am also the author of d3lines, a JavaScript library designed to display simple line charts with a few lines of code. In addition to my technical experience, here is what a Ph.D in applied optics and 7 years of work at some of the best universities in the world gave me: team spirit as well as ability to work independently, initiative, project management, design and innovation, resourcefulness, dedication, ability to meet deadlines and to write documentation and most importantly, the ability to solve any problem. Beside web design, another hobby of mine is photography. I have taken photo classes at the International Center for Photography in New York City and at Yale University: this gives me an eye for composition and layout as well as some graphical knowledge.

Updated on June 04, 2022

Comments

  • Julien Spronck
    Julien Spronck almost 2 years

    I'm building a website that uses geolocation in HTML5 to provide a service specific to the region in which the user is. Here is what I have been doing: I use the Javascript in this SO question to get country and administrative region names. Then I search the country and region names in a database and it returns location-specific data that I can display.

    Here is the script:

    <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script>
        var region = "";
        var country = "";
        function getLocation()
        {
            if (navigator.geolocation)
              {
                  navigator.geolocation.getCurrentPosition(showPosition,noGeolocation);
              } else {
                  ...
              }
        }
        function showPosition(position)
        {
            var geocoder = new google.maps.Geocoder();
            var latlong = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
            geocoder.geocode({'latLng': latlong}, function(results, status) {
                  if (status == google.maps.GeocoderStatus.OK) {
                      if (results[0]) {
                        for (var i = 0; i < results[0].address_components.length; i++)
                        {
                            var longname = results[0].address_components[i].long_name;
                            var type = results[0].address_components[i].types;
                            if (type.indexOf("administrative_area_level_1") != -1)
                            {
                                region = longname;
                            }
                            if (type.indexOf("country") != -1)
                            {
                                country = longname;
                            }
                          }
                  }
            });
        }
        function noGeolocation()
        {
            ...
        }
    
        getLocation();
    </script>
    

    This script is working fine. I had no problem with it, until I used an OS and browser set to a different languages. The script then returned the country and region names in that language. Of course, I couldnt find any match in my database.

    So, the question(s) is (are): is there a way to get language-independent country and region codes from google reverse geolocation? or is there a way to always get it in english? am I better off using IP geolocation then? or should I use a totally different method?

    Thanks for the help.