// ==UserScript==
// @name WME GIS Locator
// @namespace http://tampermonkey.net/
// @version 2018.03.11.01
// @description opens associated county GIS map and takes you to the latitude and longitude you were at (in WME) on the GIS map just opened.
// @author ramblinwreck_81
// @include https://www.waze.com/editor*
// @exclude https://www.waze.com/user/editor*
// @grant none
// @require https://gf.qytechs.cn/scripts/24851-wazewrap/code/WazeWrap.js?version=229392
// @locale English
// This script requires WME GIS Buttons script installed
// ==/UserScript==
(function() {
'use strict';
var settings = {};
function bootstrap(tries) {
tries = tries || 1;
if (W && W.map &&
W.model && W.loginManager.user &&
$ ) {
console.log('initializing WMEGL');
WMEGLinit();
} else if (tries < 1000)
setTimeout(function () {bootstrap(tries++);}, 200);
}
function WMEGLinit() {
// setTimeout(init,8000);
init();
}
function init()
{
var $section = $("<div>");
$section.html([
'<div>',
'<h2>GIS Locator</h2>',
'<input type="checkbox" id="WMEGLEnabled" class="WMEGLSettingsCheckbox"><label for="WMEGLEnabled">Enable This Script</label>',
'<hr>',
'<input type="checkbox" id="WMEGLopen2ndWMEWindow" class="WME2window"><label for="WMEGL2ndWin">Open Second WME Window on Locate</label>',
'<hr>',
'<div>',
'<h3>Last latitude and longitude Info</h3>',
'Latitude: <span id="WMEGLlatitude"></span></br>',
'Longitude: <span id="WMEGLlongitude"></span></br>',
'</div>',
'</div>'
].join(' '));
new WazeWrap.Interface.Tab('GIS Locator', $section.html(), initializeSettings);
addLocateButton();
function addLocateButton () {
var WMEGLy = document.createElement('div');
WMEGLy.setAttribute('id', 'lat-long-info');
WMEGLy.setAttribute('style', 'display:inline;');
document.getElementById('sidepanel-gislocator').appendChild(WMEGLy);
var WMEGLbb = document.createElement('button');
WMEGLbb.setAttribute('type', 'button');
WMEGLbb.setAttribute('value', 'Submit');
WMEGLbb.setAttribute('id', 'WME-GIS-locator');
WMEGLbb.setAttribute('title', 'initiate WME GIS Locator script');
WMEGLbb.innerHTML = 'Locate';
document.getElementById('lat-long-info').appendChild(WMEGLbb);
document.getElementById("WME-GIS-locator").style.height="20px";
document.getElementById("WME-GIS-locator").style.width="50px";
document.getElementById('WME-GIS-locator').style.padding='1px';
document.getElementById("WME-GIS-locator").addEventListener("click",localize, false);
if(settings.Enabled) {
document.getElementById('WME-GIS-locator').disabled = false;
} else {
document.getElementById('WME-GIS-locator').disabled = true;
}
} // end of addLocateButton function
function localize()
{
var WMEGLlatTimer;
var WMEGLgetLat = false;
document.getElementById('WME-GIS-locator').disabled = true;
function get4326CenterPoint()
{
let projI = new OL.Projection("EPSG:900913");
let projE = new OL.Projection("EPSG:4326");
let center_lonlat = (new OL.LonLat(W.map.center.lon, W.map.center.lat)).transform(projI,projE);
let lat = Math.round(center_lonlat.lat * 1000000) / 1000000;
let lon = Math.round(center_lonlat.lon * 1000000) / 1000000;
document.getElementById('WMEGLlatitude').innerHTML = lat;
document.getElementById('WMEGLlongitude').innerHTML = lon;
return new OL.LonLat(lon, lat);
}
let latlon = get4326CenterPoint();
document.getElementById('county_gis').click();
if(settings.open2ndWMEWindow) {
var WMEGLurl = 'https://www.waze.com/en-US/editor/?env=usa&lon=' + latlon.lon + '&lat=' + latlon.lat + '&zoom=4';
window.open(WMEGLurl,'wmeDup');
}
WMEGLcreateElements();
function WMEGLthisTimeLat() {
clearInterval(WMEGLlatTimer);
document.getElementById('WME-GIS-locator').disabled = false;
document.getElementById("grab-long-lat").removeEventListener('click',coordinates, false);
var removeElement = document.getElementById("grab-long-lat");
removeElement.parentNode.removeChild(removeElement);
removeElement = document.getElementById('long-and-lat-txt');
removeElement.parentNode.removeChild(removeElement);
} // end of WMEGLthisTimeLat function
function WMEGLcreateElements()
{
var WMEGLz=document.createElement("button");
WMEGLz.setAttribute("type", "button");
WMEGLz.setAttribute("value", "Submit");
WMEGLz.setAttribute("id","grab-long-lat");
WMEGLz.setAttribute("title","Get Long");
WMEGLz.innerHTML = "Get Longitude";
var WMEGLaa=document.createElement('textArea');
WMEGLaa.setAttribute("id", "long-and-lat-txt");
WMEGLaa.textContent = "";
document.getElementById("lat-long-info").appendChild(WMEGLaa);
document.getElementById("lat-long-info").appendChild(WMEGLz);
document.getElementById("grab-long-lat").style.height="20px";
document.getElementById("grab-long-lat").style.width="100px";
} // end of WMGGLcreateElements function
function coordinates()
{
if(WMEGLgetLat !==true) {
document.getElementById("long-and-lat-txt").textContent = latlon.lon;
document.getElementById("long-and-lat-txt").select();
document.execCommand("copy");
var WMEGLa = document.getElementById("grab-long-lat");
WMEGLa.innerHTML = "Get Latitude";
WMEGLgetLat = true;
} else {
document.getElementById("long-and-lat-txt").textContent = latlon.lat;
document.getElementById("long-and-lat-txt").select();
document.execCommand("copy");
WMEGLlatTimer = setInterval(function(){WMEGLthisTimeLat();},10000);
}
} //end of coordinates function
document.getElementById("grab-long-lat").addEventListener("click",coordinates, false);
} // end of localize function
} // end of init function
function initializeSettings()
{
loadSettings();
setChecked('WMEGLEnabled', settings.Enabled);
setChecked('WMEGLopen2ndWMEWindow', settings.open2ndWMEWindow);
$('#WMEGLlatitude').text('');
$('#WMEGLlongitude').text('');
$('.WMEGLSettingsCheckbox').change(function() {
var settingName = $(this)[0].id.substr(5);
settings[settingName] = this.checked;
saveSettings();
if(settings.Enabled) {
document.getElementById('WME-GIS-locator').disabled = false;
} else {
document.getElementById('WME-GIS-locator').disabled = true;
}
console.log(settingName + ' checkbox change saved');
});
$('#WMEGLopen2ndWMEWindow').change(function() {
settings.open2ndWMEWindow = this.checked;
saveSettings();
});
}
function setChecked(checkboxId, checked)
{
$('#' + checkboxId).prop('checked', checked);
}
function saveSettings()
{
if (localStorage) {
var localsettings = {
Enabled: settings.Enabled,
open2ndWMEWindow: settings.open2ndWMEWindow,
};
localStorage.setItem("WMEGL_Settings", JSON.stringify(localsettings));
}
if(settings.Enabled) {
document.getElementById('WME-GIS-locator').disabled = false;
} else {
document.getElementById('WME-GIS-locator').disabled = true;
}
}
function loadSettings()
{
var loadedSettings = $.parseJSON(localStorage.getItem("WMEGL_Settings"));
var defaultSettings = {
Enabled: false,
open2ndWMEWindow: false,
};
settings = loadedSettings ? loadedSettings : defaultSettings;
for (var prop in defaultSettings) {
if (!settings.hasOwnProperty(prop))
settings[prop] = defaultSettings[prop];
}
}
bootstrap();
})();