WME GIS Buttons

Displays the locality of the current location and provides links to open GIS if available

当前为 2018-09-22 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name          WME GIS Buttons
// @author        @Philistine11
// @namespace     https://greasyfork.org/en/users/53803
// @description   Displays the locality of the current location and provides links to open GIS if available
// @include       https://*.waze.com/editor*
// @include       https://*.waze.com/*/editor*
// @version       1.5.1
// ==/UserScript==

function gis_init(model, modeId) {
    W.app.modeController.model.bind('change:mode', gis_init);
    if (modeId === 1)
        return;

    var location = $('div.location-info-region');
    if (location.length === 0) {
        setTimeout(gis_init, 500);
        return;
    }
    location.after('<div class="input-group input-group-sm" style="float:left; padding-left:2rem;"><span class="input-group-addon" style="display:table-cell; font-size:2rem; line-height:0; width:0;"><span id="gisStatus" class="fa fa-spinner fa-pulse" style="line-height:0;"></span></span><div class="input-group-btn" style="width:0;"><a id="gisLocality" class="btn btn-default disabled hidden" style="border:1px solid" target="_blank" href="#">Locality</a><a id="gisCounty" class="btn btn-default disabled hidden" style="border:1px solid" target="_blank" href="#">County</a><a id="gisState" class="btn btn-default disabled hidden" style="border:1px solid" target="_blank" href="#">State</a></span></div>');

    var GISButtonsOn = JSON.parse(localStorage.getItem('GISButtonsOn'));
    var GISButtonsAPIKey = localStorage.getItem('GISButtonsAPIKey');
    $('#gisStatus').click(() => {
        GISButtonsOn = !GISButtonsOn;
        localStorage.setItem('GISButtonsOn', GISButtonsOn);
        start();
    }).contextmenu(() => {
        do {
            GISButtonsAPIKey = prompt("Enter your GIS Buttons API key:", GISButtonsAPIKey || "").trim();
        } while (!GISButtonsAPIKey || GISButtonsAPIKey.length != 39);
        localStorage.setItem('GISButtonsAPIKey', GISButtonsAPIKey);
        start();
        return false;
    });

    var trigger;
    var setTrigger = () => {if (GISButtonsAPIKey) trigger = setTimeout(update, 1000);}
    var clearTrigger = () => clearTimeout(trigger);

    var states = {};
    var start = () => {
        new Promise((resolve, reject) => {
            if (Object.keys(states).length == 0)
                $.getJSON('https://script.google.com/macros/s/AKfycbx2bytvT5Un0TWcaU7BpVkauqeE8zqt8Mek7Zq-OF-bznGYDyZw/exec?link=10dR8z16eKPHeI-ywLcHh2UNS3enQ7gt36Hhzm9nOJbA', rows => {
                    for (var row in rows)
                        states[rows[row][0]] = rows[row][1];
                });
            resolve();
        }).then(() =>{
            if (GISButtonsOn) {
                if (GISButtonsAPIKey) {
                    W.map.events.register('movestart', null, clearTrigger);
                    W.map.events.register('moveend', null, setTrigger);
                    update();
                } else
                    $('#gisStatus').removeClass().addClass('fa fa-key').css('color','red').attr('title',"Right-click to set GIS Buttons API Key");
            } else {
                W.map.events.unregister('movestart', null, clearTrigger);
                W.map.events.unregister('moveend', null, setTrigger);
                $('#gisLocality, #gisCounty, #gisState').addClass('hidden');
                $('#gisStatus').removeClass().addClass('fa fa-power-off').css('color','red').attr('title',"Click to turn on GIS Buttons");
            }
        });
    }

    var update = () => {
        $('#gisStatus').removeClass().addClass('fa fa-spinner fa-pulse').css('color','').attr('title','');
        var center = W.map.getCenter().transform(new OL.Projection('EPSG:900913'), new OL.Projection('EPSG:4326'));
        $.getJSON(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${center.lat},${center.lon}&key=${GISButtonsAPIKey}`, data => {
            if (data.status === 'OK') {
                var locs = data.results[0].address_components;
                var locality = '', county = '', state = '';
                $('#gisLocality, #gisCounty, #gisState').addClass('hidden');
                for (var loc = 0; loc < locs.length; loc++) {
                    if (locs[loc].types.indexOf('administrative_area_level_1') !== -1) {
                        state = locs[loc].long_name;
                        $('#gisState').removeClass('hidden').text(state);
                    } else if (locs[loc].types.indexOf('administrative_area_level_2') !== -1) {
                        county = locs[loc].long_name;
                        $('#gisCounty').removeClass('hidden').text(county);
                    } else if (locs[loc].types.indexOf('locality') !== -1) {
                        locality = locs[loc].long_name;
                        $('#gisLocality').removeClass('hidden').text(locality);
                    }
                }

                $('#gisLocality, #gisCounty, #gisState').prop('href', '#').addClass('disabled');

                new Promise((resolve, reject) => {
                    if (states.hasOwnProperty(state)) {
                        resolve(new Promise((resolve, reject) => {
                            if (typeof (states[state]) === 'string')
                                $.getJSON(`https://script.google.com/macros/s/AKfycbx2bytvT5Un0TWcaU7BpVkauqeE8zqt8Mek7Zq-OF-bznGYDyZw/exec?link=${states[state]}`, rows => {
                                    states[state] = rows;
                                    resolve();
                                });
                            else
                                resolve();
                        }).then(() => {
                            for (var row in states[state])
                                if (states[state][row][2] !== '')
                                    if (states[state][row][1] === 'State') {
                                        $('#gisState').prop('href', states[state][row][2].replace('<lat>',center.lat).replace('<lon>',center.lon).replace('<zoom>',W.map.getZoom()+12)).removeClass('disabled');
                                    } else if (states[state][row][1] === 'County') {
                                        if (county.indexOf(states[state][row][0]) != -1)
                                            $('#gisCounty').prop('href', states[state][row][2].replace('<lat>',center.lat).replace('<lon>',center.lon).replace('<zoom>',W.map.getZoom()+12)).removeClass('disabled');
                                    } else if (states[state][row][0] === locality)
                                        $('#gisLocality').prop('href', states[state][row][2].replace('<lat>',center.lat).replace('<lon>',center.lon).replace('<zoom>',W.map.getZoom()+12)).removeClass('disabled');
                        }));
                    }
                    resolve();
                }).then(() => $('#gisStatus').removeClass().addClass('fa fa-power-off').css('color','green').attr('title',"Click to turn off GIS Buttons")).catch(error => console.warn(error));
            } else {
                console.error("GIS Buttons", data);
                $('#gisStatus').removeClass().addClass('fa fa-exclamation-circle').css('color','red').attr('title', "Check console for errors");
            }
        });
    };

    start();
}

gis_init();