Get city name using redirect geolocation

Asked

Viewed 740 times

1

<!DOCTYPE html>
<html>
<head>
<title>GEOIP DB - jQuery example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<div>Country: <span id="country"></span>
<div>State: <span id="state"></span>
<div>City: <span id="city"></span>
<div>Latitude: <span id="latitude"></span>
<div>Longitude: <span id="longitude"></span>
<div>IP: <span id="ip"></span>
<script>
    $.ajax({
        url: "https://geoip-db.com/jsonp",
        jsonpCallback: "callback",
        dataType: "jsonp",
        success: function( location ) {
            $('#country').html(location.country_name);
            $('#state').html(location.state);
            $('#city').html(location.city);
            $('#latitude').html(location.latitude);
            $('#longitude').html(location.longitude);
            $('#ip').html(location.IPv4);  
        }
           {
 "Los Angeles": "http://exemplo.com/LA", 
 "New York": "http://exemplo.com/NY", 
 "Other City": "http://exemplo.com/othercity"
}
    });     
</script>
</body>
</html>

Example: Access http://Exemplo.com get the city "New York" redirects to http://Exemplo.com/NY or if it’s "Los Angeles" redirect to http://Exemplo.com/LA? And if it were any other city, it would go to a third URL. Is there any way I could do that I tried and I couldn’t.

  • What you’ve tried, that you haven’t succeeded in

  • Add routes to a variable. Ex: routes = {"cidade1": "http://rota1.com", "cidade2": "http://rota2.com"} etc. Then just take the value of location.city and check if there is array created. Ex: if ( routes[location.city] ) { window.location.href = routes[location.city] }

  • Thanks Valdeir tried, but I get no results. I’m beginner in jquery.

1 answer

2

Following what our friend Valdeirpsr spoke, just create a variavel with the cities and their links, and check after the success of the requisition ajax

routes = {"Los Angeles": "http://exemplo.com/LA", "New York": "http://exemplo.com/NY", "Other City": "http://exemplo.com/othercity", "São Paulo": "http://google.com/SP"};
<!DOCTYPE html>
<html>
<head>
<title>GEOIP DB - jQuery example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<div>Country: <span id="country"></span>
<div>State: <span id="state"></span>
<div>City: <span id="city"></span>
<div>Latitude: <span id="latitude"></span>
<div>Longitude: <span id="longitude"></span>
<div>IP: <span id="ip"></span>
<script>
    $.ajax({
        url: "https://geoip-db.com/jsonp",
        jsonpCallback: "callback",
        dataType: "jsonp",
        success: function( location ) {
            $('#country').html(location.country_name);
            $('#state').html(location.state);
            $('#city').html(location.city);
            $('#latitude').html(location.latitude);
            $('#longitude').html(location.longitude);
            $('#ip').html(location.IPv4);  
            
            if(routes[location.city]){
              alert('tem sua cidade no array! => link salvo no array para redirecionar: '+ routes[location.city]);
              window.location.href = routes[location.city];
            }
            
        }          
    });     
</script>

</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.