MV [solved]GPS location plugin

Code:
//=============================================================================
// GeoCachingPlugin.js
//=============================================================================

/*:
 * @plugindesc Geocaching Plugin for RPG Maker MV
 * This plugin allows you to create a geocaching mini-game using GPS coordinates.
 * @author Squareware
 *
 * @param Proximity Threshold
 * @desc Proximity threshold for triggering events (in kilometers). Default is 0.02 km (20 meters).
 * @default 0.02
 *
 * @param Latitude Variable ID
 * @desc Game Variable ID to store the latitude of the target location. Default is 1.
 * @default 1
 *
 * @param Longitude Variable ID
 * @desc Game Variable ID to store the longitude of the target location. Default is 2.
 * @default 2
 *
 * @param User Latitude Variable ID
 * @desc Game Variable ID to store the user's latitude. Default is 3.
 * @default 3
 *
 * @param User Longitude Variable ID
 * @desc Game Variable ID to store the user's longitude. Default is 4.
 * @default 4
 *
 * @param Switch ID
 * @desc Game Switch ID to turn on when the location is reached. Default is 1.
 * @default 1
 *
 * @help
 * Plugin Commands:
 * - GeoCachingSetLocation latitude longitude
 *   Sets the target location for the geocaching game. Replace latitude and longitude with the desired coordinates.
 */

(function() {
    var parameters = PluginManager.parameters('GeoCachingPlugin');

    var proximityThreshold = parseFloat(parameters['Proximity Threshold'] || 0.02); // Default is 0.02 km (20 meters)
    var latitudeVariableId = parseInt(parameters['Latitude Variable ID'] || 1);
    var longitudeVariableId = parseInt(parameters['Longitude Variable ID'] || 2);
    var userLatitudeVariableId = parseInt(parameters['User Latitude Variable ID'] || 3);
    var userLongitudeVariableId = parseInt(parameters['User Longitude Variable ID'] || 4);
    var switchId = parseInt(parameters['Switch ID'] || 1);

    // Constants
    const EARTH_RADIUS_KM = 6371; // Earth's radius in kilometers

    // Handle the "GeoCachingSetLocation" command
    function handleSetLocationCommand(params) {
        if (params.length === 2) {
            // Extract latitude and longitude values from the command parameters
     var targetLatitude = parseFloat(params[0]).toFixed(6); // Preserve 6 decimal places
        var targetLongitude = parseFloat(params[1]).toFixed(6); // Preserve 6 decimal places

            // Debugging output
            console.log('Target Latitude Set:', targetLatitude);
            console.log('Target Longitude Set:', targetLongitude);

            // Store the target coordinates in the specified game variables
            $gameVariables.setValue(latitudeVariableId, targetLatitude);
            $gameVariables.setValue(longitudeVariableId, targetLongitude);
        } else {
            console.error("Invalid parameters for GeoCachingSetLocation command.");
        }
    }

    // Plugin Command handling
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        if (command === 'GeoCachingSetLocation') {
            handleSetLocationCommand(args);
        }
    };

    // Initialize geolocation when the game is ready.
    var _Scene_Map_start = Scene_Map.prototype.start;
    Scene_Map.prototype.start = function() {
        _Scene_Map_start.call(this);
        if ('geolocation' in navigator) {
            navigator.geolocation.getCurrentPosition(onLocationSuccess, onLocationError);
        }
    };

    // Callback for successful geolocation.
    function onLocationSuccess(position) {
        var userLatitude = position.coords.latitude;
        var userLongitude = position.coords.longitude;

        // Store the user's coordinates in the specified game variables
        $gameVariables.setValue(userLatitudeVariableId, userLatitude);
        $gameVariables.setValue(userLongitudeVariableId, userLongitude);

        // Retrieve the target coordinates from game variables
        var targetLatitude = $gameVariables.value(latitudeVariableId);
        var targetLongitude = $gameVariables.value(longitudeVariableId);

        // Debugging output
        console.log('User Latitude:', userLatitude);
        console.log('User Longitude:', userLongitude);
        console.log('Target Latitude:', targetLatitude);
        console.log('Target Longitude:', targetLongitude);

        // Calculate proximity based on latitude and longitude differences
        var latDiffDegrees = targetLatitude - userLatitude;
        var lonDiffDegrees = targetLongitude - userLongitude;

        // Convert latitude and longitude differences to radians
        var latDiffRadians = latDiffDegrees * (Math.PI / 180);
        var lonDiffRadians = lonDiffDegrees * (Math.PI / 180);

        // Calculate the Haversine formula
        var a = Math.sin(latDiffRadians / 2) * Math.sin(latDiffRadians / 2) +
            Math.cos(userLatitude * (Math.PI / 180)) * Math.cos(targetLatitude * (Math.PI / 180)) *
            Math.sin(lonDiffRadians / 2) * Math.sin(lonDiffRadians / 2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        var distance = EARTH_RADIUS_KM * c;

        // Debugging output
        console.log('Latitude Difference (radians):', latDiffRadians);
        console.log('Longitude Difference (radians):', lonDiffRadians);
        console.log('Calculated Distance (km):', distance);
        console.log('Proximity Threshold (km):', proximityThreshold);

        if (distance <= proximityThreshold){
            // Turn on the specified switch
            $gameSwitches.setValue(switchId, true);
        } else {
            // Turn off the specified switch
            $gameSwitches.setValue(switchId, false);
        }
    }

    // Callback for geolocation error.
    function onLocationError(error) {
        console.error("Geolocation error: " + error.message);
    }
})();
[Solved]

Original thread: https://forums.rpgmakerweb.com/threads/solved-gps-location-plugin.162028/