analytics pixel

Redirect Based on Geolocation: A Code Nuggets for WebSite X5 - Blog: Insights and Inspiration - Gebher | Digital Company

Redirect Based on Geolocation

Go to content

Redirect Based on Geolocation

Gebher | Digital Company
Published by Gebher Editorial Board in Code Nuggets · 11 February 2023
Tags: GeolocationRedirectCode

Geotargeting is a technology that allows you to customize navigation based on your geographic location. This can be useful for providing personalized content, region-specific offers, and more. In this post, I'm going to show you how to create simple location-based redirect code using JavaScript and the navigator.geolocation method.


This script uses the browser's geolocation API to get the user's current location. Once your location has been determined, the distance between your location and a specific point (London, in this case) is calculated using a distance calculator function. Finally, if the distance between the user's location and the specific point is greater than a certain radius (50 km, in this case), the user is redirected to an alert page.


Benefits

  • It allows you to customize the user experience based on your geographic location;
  • Easy to use and understand;
  • It can be used for content targeting or advertising.

Disadvantages

  • Requires user permission to access its location;
  • Not all browsers support geolocation;
  • Location accuracy can be affected by external factors such as GPS coverage and the presence of obstacles.

JavaScript
  1. if (navigator.geolocation) {
  2.   navigator.geolocation.getCurrentPosition(function(position) {
  3.     var latitude = position.coords.latitude;
  4.     var longitude = position.coords.longitude;
  5.  
  6.     // London coordinates
  7.     var londonLat = 51.509865;
  8.     var londonLng = -0.118092;
  9.  
  10.     // Calculates the distance between the user's location and London
  11.     var distance = calculateDistance(latitude, longitude, londonLat, londonLng);
  12.  
  13.     // If the distance is greater than a certain radius, it redirects to the warning page
  14.     if (distance > 50) {
  15.       window.location.href = "/warning.html";
  16.     }
  17.   });
  18. }
  19.  
  20. // Function to calculate the distance between two geographical points
  21. function calculateDistance(lat1, lon1, lat2, lon2) {
  22.   var R = 6371; // radius of the Earth in km
  23.   var dLat = (lat2 - lat1) * (Math.PI / 180);
  24.   var dLon = (lon2 - lon1) * (Math.PI / 180);
  25.   var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
  26.     Math.cos(lat1 * (Math.PI / 180)) * Math.cos(lat2 * (Math.PI / 180)) *
  27.     Math.sin(dLon / 2) * Math.sin(dLon / 2);
  28.   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  29.   var d = R * c;
  30.   return d;
  31. }

This script is highly versatile and can be used in many situations where there is a need to tailor the user experience based on geographic location. For example, it can be used to show region-specific content, offer local promotions, or restrict access to content based on your geographic location.


Also, this script can easily be adapted to work with other cities or locations. All that is required is to replace the London coordinates with the desired ones and adjust the radius as needed.


It is also important to note that this script uses a function to calculate the distance between two geographic points based on coordinates. This function uses a distance calculation algorithm known as "Haversine's theorem", which allows you to calculate the distance between two points on the surface of a sphere (such as the Earth) accurately.


In summary, this script provides a simple and flexible solution to customize user experience based on geographic location, making it an ideal solution for many web applications.



5.0 / 5
1 review
1
0
0
0
0
17 Jul 2023
Ottimo articolo e molto interessante!
Back to content