You doubt how to get a user's location

1

I know that the stackoverflow community are strict with the questions but I have a need and I have never worked with geolocation , the thing is that I would like to know if there is a way to get a user's location by means of an event that is to say I have the following example code of what I need to do ..

<select id="countries">
<option value="United Stated">United Stated</option>
<option value="Canada">Canada</option>
<option value="Spain">Spain</option>
</select>

js file

$("#countries").on("change",function(){

    /*en el evento on change del select me gustaria determinar 
      si el pais que el usuario elegio es verdaderamente su pais
      es decir que determine su location mediante on change
      y si no es su pais en verdad me mande una alerta con alguna 
       advertencia 
    */

})

Any recommendations on how I could do what I want? If there is a library or framework that helps me with what I need to do, it would be great. if not someone could illuminate me an idea that methods exist to do what I want to be in javascript or php any tip I would appreciate a lot of thanks!

    
asked by andy gibbs 18.09.2018 в 19:56
source

2 answers

1

It's not so trivial. The HTML5 GeoLocation API allows you to make a call of the type:

navigator.geolocation.getCurrentPosition(function(position) {
  console.log('la posición es', position);
}, function(err) {
  console.warn(err);
});

That, in the best of cases (the browser supports geolocation, the browser authorizes you to request geolocation -google only allows domains served with https- and the user agrees to share your position) will return an instance of Position which in turn has coordinates.

To associate these coordinates to a country you would have to look for a service that offers that type of API. Google Maps has an endpoint to do Reverse Geocoding that accepts a pair of coordinates and returns an object that Contains country and administrative subdivisions (optionally zipcode and address). You need an API Key to test it.

    
answered by 18.09.2018 в 21:29
0

I found a way to do it with a super good api that gets the type of ip addresses of all countries,

As they say by ahy, it is important to investigate, I share what I did, what can I do for them in some project.

$(document).ready(function(){

$.getJSON('https://ipapi.co/json/', function(data) {
  JSON.stringify(data, null, 2);

 var country =data.country_name;

   

$("#countries").change(function(){
var c=$(this).val();


    if(c!=country ){
    
      alert(c+" is no your country")
       $("#response").html("your country is: "+country )
    
    }else{
    
      alert(c+" is your country Thank you")
       $("#response").html("your country is: "+country )
    
    }
  

    
    })
    
});

})
<select id="countries">
<option value="United Stated">United Stated</option>
<option value="Canada">Canada</option>
<option value="Spain">Spain</option>
<option value="Dominican Republic">Dominican Republic</option>
</select>


<div id="response"></div>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
answered by 19.09.2018 в 15:40