Get city name using geolocation with redirection

0

<!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:<spanid="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 link get the city     "New York" redirects to link     or is it "Los Angeles" redirecting to link ? And if it was any other city, it would be directed to a 3rd URL.     Is it possible to do this I tried and could not.

    
asked by anonymous 02.02.2018 / 18:42

1 answer

0

Following what our friend ValdeirPsr spoke, just create a variavel with cities and their links, and check after success of request 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.br/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:<spanid="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>
    
05.02.2018 / 12:32