OpenStreetMap to Google Maps

Add button to redirect to Google Maps from OpenStreetMap and allow dragging

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         OpenStreetMap to Google Maps
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Add button to redirect to Google Maps from OpenStreetMap and allow dragging
// @author       Aoi
// @match        https://www.openstreetmap.org/edit*
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';

    // Retrieve button position from storage or set default
    var buttonX = GM_getValue('buttonX', '50%');
    var buttonY = GM_getValue('buttonY', '20px');

    // Create button element
    var googleMapsButton = document.createElement('button');
    googleMapsButton.textContent = 'View on Google Maps';
    googleMapsButton.style.position = 'fixed';
    googleMapsButton.style.left = buttonX;
    googleMapsButton.style.top = buttonY;
    googleMapsButton.style.zIndex = '9999';
    googleMapsButton.style.cursor = 'move';
    googleMapsButton.style.padding = '10px';
    googleMapsButton.style.backgroundColor = '#fff';
    googleMapsButton.style.border = '1px solid #000';
    googleMapsButton.style.borderRadius = '5px';

    // Function to handle button drag
    function handleDrag(event) {
        event.preventDefault();
        var offsetX = event.clientX - startX;
        var offsetY = event.clientY - startY;
        var newX = buttonStartX + offsetX;
        var newY = buttonStartY + offsetY;
        googleMapsButton.style.left = newX + 'px';
        googleMapsButton.style.top = newY + 'px';
        GM_setValue('buttonX', newX + 'px');
        GM_setValue('buttonY', newY + 'px');
    }

    // Function to handle mouse release
    function handleRelease(event) {
        document.removeEventListener('mousemove', handleDrag);
        document.removeEventListener('mouseup', handleRelease);
    }

    // Variables to track mouse movement
    var startX, startY, buttonStartX, buttonStartY;

    // Event listener for mouse down on the button
    googleMapsButton.addEventListener('mousedown', function(event) {
        event.preventDefault();
        startX = event.clientX;
        startY = event.clientY;
        buttonStartX = googleMapsButton.offsetLeft;
        buttonStartY = googleMapsButton.offsetTop;
        document.addEventListener('mousemove', handleDrag);
        document.addEventListener('mouseup', handleRelease);
    });

    // Event listener for button click
    googleMapsButton.addEventListener('click', function() {
        // Get the latitude, longitude, and zoom from the URL
        var url = window.location.href;
        var matches = url.match(/#map=(\d+)\/([-\d.]+)\/([-\d.]+)/);
        if (matches && matches.length >= 4) {
            var zoom = matches[1];
            var lat = matches[2];
            var lon = matches[3];

            // Construct the Google Maps URL and open in new tab
            var googleMapsURL = `https://www.google.com/maps/@${lat},${lon},${zoom}z`;
            window.open(googleMapsURL, '_blank');
        }
    });

    // Append button to the document body
    document.body.appendChild(googleMapsButton);
})();