DevByPowerPoint

ELHC 459 | Allow Location?

Oct 14th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.01 KB | Source Code | 0 0
  1. // Get a reference to the player object
  2. var player = GetPlayer();
  3.  
  4. // Function to handle successful geolocation
  5. function successCallback(position) {
  6.     // Extract latitude and longitude from the position object, rounded to 3 decimal places
  7.     var latitude = position.coords.latitude.toFixed(3);
  8.     var longitude = position.coords.longitude.toFixed(3);
  9.    
  10.     // Create a string with the latitude and longitude
  11.     var locationString = "Latitude: " + latitude + ", Longitude: " + longitude;
  12.    
  13.     // Set the location string to a variable in the player object
  14.     player.SetVar("vUserLocation", locationString);
  15.    
  16.     // Create a new XMLHttpRequest object for making an HTTP request
  17.     var xhr = new XMLHttpRequest();
  18.    
  19.     // Construct the URL for reverse geocoding using OpenStreetMap's Nominatim service
  20.     var url = "https://nominatim.openstreetmap.org/reverse?format=json&lat=" + latitude + "&lon=" + longitude;
  21.    
  22.     // Open a GET request to the constructed URL
  23.     xhr.open("GET", url, true);
  24.    
  25.     // Set up a function to handle the response from the server
  26.     xhr.onreadystatechange = function() {
  27.         // Check if the request is complete and successful
  28.         if (xhr.readyState === 4 && xhr.status === 200) {
  29.             // Parse the JSON response
  30.             var response = JSON.parse(xhr.responseText);
  31.            
  32.             // If address information is available in the response
  33.             if (response.address) {
  34.                 // Extract various address components, using fallbacks if certain fields are not available
  35.                 var country = response.address.country || "";
  36.                 var city = response.address.city || response.address.town || response.address.village || "";
  37.                 var postcode = response.address.postcode || "";
  38.                 var state = response.address.state || "";
  39.                 var street = response.address.road || "";
  40.                
  41.                 // Set the extracted address components to variables in the player object
  42.                 player.SetVar("vCountry", country);
  43.                 player.SetVar("vCity", city);
  44.                 player.SetVar("vPostcode", postcode);
  45.                 player.SetVar("vState", state);
  46.                 player.SetVar("vStreet", street);
  47.             }
  48.         }
  49.     };
  50.    
  51.     // Send the HTTP request
  52.     xhr.send();
  53. }
  54.  
  55. // Function to handle geolocation errors
  56. function errorCallback(error) {
  57.     // Create an error message
  58.     var errorMessage = "Error retrieving location: " + error.message;
  59.    
  60.     // Set the error message to a variable in the player object
  61.     player.SetVar("vError", errorMessage);
  62. }
  63.  
  64. // Check if geolocation is supported by the browser
  65. if (navigator.geolocation) {
  66.     // If supported, request the current position
  67.     navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
  68. } else {
  69.     // If not supported, set an error message in the player object
  70.     player.SetVar("vError", "Geolocation is not supported by this browser.");
  71. }
Advertisement
Add Comment
Please, Sign In to add comment