WME GIS Buttons

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

当前为 2019-10-31 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME GIS Buttons
  3. // @author @Philistine11
  4. // @namespace https://greasyfork.org/en/users/53803
  5. // @description Displays the locality of the current location and provides links to open GIS if available
  6. // @match *://*.waze.com/*editor*
  7. // @exclude *://*.waze.com/user/editor*
  8. // @version 1.5.3
  9. // ==/UserScript==
  10. /* global $, W, OL */
  11.  
  12. async function gis_init() {
  13. let gisButtonsOn = JSON.parse(localStorage.getItem('gisButtonsOn'));
  14. let gisButtonsApiKey = localStorage.getItem('gisButtonsApiKey');
  15. const gisButtons = $('<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>');
  16.  
  17. gisButtons.find('#gisStatus').click(() => {
  18. gisButtonsOn = !gisButtonsOn;
  19. localStorage.setItem('gisButtonsOn', gisButtonsOn);
  20. start();
  21. }).contextmenu(() => {
  22. do {
  23. gisButtonsApiKey = prompt("Enter your GIS Buttons API key:", gisButtonsApiKey || "").trim();
  24. } while (!gisButtonsApiKey || gisButtonsApiKey.length != 39);
  25. localStorage.setItem('gisButtonsApiKey', gisButtonsApiKey);
  26. start();
  27. return false;
  28. });
  29.  
  30. let trigger;
  31. const setTrigger = () => {if (gisButtonsApiKey) trigger = setTimeout(update, 1000);}
  32. const clearTrigger = () => clearTimeout(trigger);
  33.  
  34. const states = {};
  35. const rows = await $.getJSON('https://script.google.com/macros/s/AKfycbx2bytvT5Un0TWcaU7BpVkauqeE8zqt8Mek7Zq-OF-bznGYDyZw/exec?link=10dR8z16eKPHeI-ywLcHh2UNS3enQ7gt36Hhzm9nOJbA');
  36. for (let row in rows) states[rows[row][0]] = rows[row][1];
  37.  
  38. const update = async () => {
  39. $('#gisStatus').removeClass().addClass('fa fa-spinner fa-pulse').css('color','').attr('title','');
  40. const center = W.map.getCenter().transform(new OL.Projection('EPSG:900913'), new OL.Projection('EPSG:4326'));
  41. const data = await $.getJSON(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${center.lat},${center.lon}&key=${gisButtonsApiKey}`);
  42. if (data.status !== 'OK') {
  43. $('#gisStatus').removeClass().addClass('fa fa-exclamation-circle').css('color','red').attr('title', "Check console for errors");
  44. return console.error("GIS Buttons", data);
  45. }
  46.  
  47. let locs = data.results.find((result) => !result.types.includes("point_of_interest")).address_components || [];
  48. let locality = '', county = '', state = '';
  49. $('#gisLocality, #gisCounty, #gisState').addClass('hidden');
  50. for (let loc = 0; loc < locs.length; loc++)
  51. if (locs[loc].types.indexOf('administrative_area_level_1') !== -1) {
  52. state = locs[loc].long_name;
  53. $('#gisState').removeClass('hidden').text(state);
  54. } else if (locs[loc].types.indexOf('administrative_area_level_2') !== -1) {
  55. county = locs[loc].long_name;
  56. $('#gisCounty').removeClass('hidden').text(county);
  57. } else if (locs[loc].types.indexOf('locality') !== -1) {
  58. locality = locs[loc].long_name;
  59. $('#gisLocality').removeClass('hidden').text(locality);
  60. }
  61.  
  62. $('#gisLocality, #gisCounty, #gisState').prop('href', '#').addClass('disabled');
  63. if (states.hasOwnProperty(state)) {
  64. if (typeof (states[state]) === 'string')
  65. states[state] = await $.getJSON(`https://script.google.com/macros/s/AKfycbx2bytvT5Un0TWcaU7BpVkauqeE8zqt8Mek7Zq-OF-bznGYDyZw/exec?link=${states[state]}`);
  66. for (let row in states[state])
  67. if (states[state][row][2] !== '')
  68. if (states[state][row][1] === 'State') {
  69. $('#gisState').prop('href', states[state][row][2].replace('<lat>',center.lat).replace('<lon>',center.lon).replace('<zoom>',W.map.getZoom()+12)).removeClass('disabled');
  70. } else if (states[state][row][1] === 'County') {
  71. if (county.indexOf(states[state][row][0]) != -1)
  72. $('#gisCounty').prop('href', states[state][row][2].replace('<lat>',center.lat).replace('<lon>',center.lon).replace('<zoom>',W.map.getZoom()+12)).removeClass('disabled');
  73. } else if (states[state][row][0] === locality)
  74. $('#gisLocality').prop('href', states[state][row][2].replace('<lat>',center.lat).replace('<lon>',center.lon).replace('<zoom>',W.map.getZoom()+12)).removeClass('disabled');
  75. }
  76. $('#gisStatus').removeClass().addClass('fa fa-power-off').css('color','green').attr('title',"Click to turn off GIS Buttons");
  77. };
  78.  
  79. function start(model, modeId) {
  80. const mapEvents = W.map.events || W.map.getMapEventsListener();
  81. if (modeId === 1) {
  82. clearTrigger();
  83. mapEvents.unregister('movestart', null, clearTrigger);
  84. mapEvents.unregister('moveend', null, setTrigger);
  85. return;
  86. }
  87.  
  88. const location = $('div.topbar:not(.topbar-mte) > div.location-info-region');
  89. if (location.length === 0) return setTimeout(start, 500);
  90. location.after(gisButtons);
  91.  
  92. if (gisButtonsOn) {
  93. if (gisButtonsApiKey) {
  94. mapEvents.register('movestart', null, clearTrigger);
  95. mapEvents.register('moveend', null, setTrigger);
  96. update();
  97. } else
  98. $('#gisStatus').removeClass().addClass('fa fa-key').css('color','red').attr('title',"Right-click to set GIS Buttons API Key");
  99. } else {
  100. clearTrigger();
  101. mapEvents.unregister('movestart', null, clearTrigger);
  102. mapEvents.unregister('moveend', null, setTrigger);
  103. $('#gisLocality, #gisCounty, #gisState').addClass('hidden');
  104. $('#gisStatus').removeClass().addClass('fa fa-power-off').css('color','red').attr('title',"Click to turn on GIS Buttons");
  105. }
  106. }
  107.  
  108. W.app.modeController.model.bind('change:mode', start);
  109. start();
  110. }
  111.  
  112. gis_init();