OpenStreetMap to Google Maps

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
})();