Add button to redirect to Google Maps from OpenStreetMap and allow dragging
当前为
// ==UserScript==
// @name OpenStreetMap to Google Maps
// @namespace http://tampermonkey.net/
// @version 0.1
// @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';
// 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 and longitude from the URL
var url = window.location.href;
var matches = url.match(/\/(\d+\.\d+)\/(\d+\.\d+)/);
if (matches && matches.length >= 3) {
var lat = matches[1];
var lon = matches[2];
// Construct the Google Maps URL and open in new tab
var googleMapsURL = 'https://www.google.com/maps/@' + lat + ',' + lon + ',16z';
window.open(googleMapsURL, '_blank');
}
});
// Append button to the document body
document.body.appendChild(googleMapsButton);
})();