Guest User

Pathpoints WORKING

a guest
Mar 9th, 2015
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. // ==UserScript==
  2. // @id iitc-plugin-waypoints
  3. // @name IITC plugin: Waypoints
  4. // @category Layer
  5. // @version 0.0.1
  6. // @author Nobody
  7. // @namespace https://github.com/jonatkins/ingress-intel-total-conversion
  8. // @description Creates paths of waypoints to plan ingress actions in more detail
  9. // @include https://www.ingress.com/intel*
  10. // @include http://www.ingress.com/intel*
  11. // @match https://www.ingress.com/intel*
  12. // @match http://www.ingress.com/intel*
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16.  
  17. function wrapper(plugin_info) {
  18. // ensure plugin framework is there, even if iitc is not yet loaded
  19. if (typeof window.plugin !== 'function') window.plugin = function () {};
  20.  
  21. // PLUGIN START ////////////////////////////////////////////////////////
  22.  
  23. window.plugin.waypoints = function () {};
  24.  
  25. window.plugin.waypoints.portalAdded = function (data) {
  26. // Marker is in data.portal
  27. // Adding the on click callback for recording the clicks on making the waypoint map
  28. data.portal.on('click', function (marker) {
  29. window.plugin.waypoints.onPortalClick(marker);
  30. });
  31. }
  32.  
  33. window.plugin.waypoints.onPortalClick = function(marker) {
  34. if (false == window.plugin.waypoints.recording) {
  35. // Skipping, only interesting when we record
  36. return;
  37. }
  38.  
  39. // Saving the relevant data in out own store, so the user can move the map, zoom how they want and
  40. // keep all the data present
  41. //console.log(marker);
  42.  
  43. var guid = marker.target.options.guid;
  44. var data = marker.target.options.data;
  45.  
  46. if (!window.plugin.waypoints.pointData[guid]) {
  47. // Not yet added
  48. window.plugin.waypoints.pointData[guid] = data;
  49. }
  50.  
  51. // This is an array, so we keep them this way to preserve the order The object is just to keep the data
  52. window.plugin.waypoints.points.push(guid);
  53.  
  54. //console.log('Added waypoint ' + data.title);
  55.  
  56. window.plugin.waypoints.updateWaypointLayer();
  57. }
  58.  
  59. window.plugin.waypoints.updateWaypointLayer = function() {
  60. var count = window.plugin.waypoints.points.length;
  61.  
  62. if (count == 0) {
  63. return; // nothing to do
  64. }
  65.  
  66. var latlngs = [];
  67. for (var i = 0; i < count; ++i) {
  68. var guid = window.plugin.waypoints.points[i];
  69. var data = window.plugin.waypoints.pointData[guid];
  70. var latlng = new L.LatLng((data.latE6 / 1E6), (data.lngE6 / 1E6));
  71.  
  72. latlngs.push(latlng);
  73. }
  74.  
  75. if (null != window.plugin.waypoints.waypointsLayer) {
  76. // Remove it
  77. window.map.removeLayer(window.plugin.waypoints.waypointsLayer);
  78. window.plugin.waypoints.waypointsLayer = null;
  79. }
  80.  
  81. var options = {
  82. lineCap: 'round',
  83. clickable: false,
  84. opacity: 0.8,
  85. weight: 6,
  86. color: '#FF0000'
  87. };
  88.  
  89. window.plugin.waypoints.waypointsLayer = L.polyline(latlngs, options).addTo(map);
  90. window.plugin.waypoints.waypointsLayer.addTo(window.plugin.waypoints.waypointsLayerGroup);
  91. }
  92.  
  93. window.plugin.waypoints.controls = L.Control.extend({
  94. options: {
  95. position: 'topleft'
  96. },
  97. initialize: function (foo, options) {
  98. L.Util.setOptions(this, options);
  99. },
  100. onAdd: function (map) {
  101. // Container
  102. var container = L.DomUtil.create('div', window.plugin.waypoints.CONTROLS_CONTAINER);
  103.  
  104. var controls = '<button id="' + window.plugin.waypoints.CONTROLS_RECORD + '" onclick="window.plugin.waypoints.record();return false;">Record</button> <button onclick="window.plugin.waypoints.dump();return false;">Dump</button> <button onclick="window.plugin.waypoints.clear();return false;">Clear</button>';
  105. $(container).html(controls);
  106.  
  107. return container;
  108. }
  109. });
  110.  
  111. window.plugin.waypoints.toggleControl = function () {
  112. if (map.hasLayer(window.plugin.waypoints.waypointsLayerGroup)) {
  113. $('.' + window.plugin.waypoints.CONTROLS_CONTAINER).show();
  114. } else {
  115. window.plugin.waypoints.recording = false; // Disable recording when hiding layer
  116. $('.' + window.plugin.waypoints.CONTROLS_CONTAINER).hide();
  117. }
  118. }
  119.  
  120. window.plugin.waypoints.record = function () {
  121. if (true == window.plugin.waypoints.recording) {
  122. // We are recording, so now this means, stop recording
  123. window.plugin.waypoints.recording = false;
  124. $('#' + window.plugin.waypoints.CONTROLS_RECORD).html('Record');
  125. } else {
  126. // Not recording, so now we DO record
  127. window.plugin.waypoints.recording = true;
  128. $('#' + window.plugin.waypoints.CONTROLS_RECORD).html('Stop');
  129. }
  130. }
  131.  
  132. window.plugin.waypoints.dump = function () {
  133. var points = [];
  134. var count = window.plugin.waypoints.points.length;
  135.  
  136. for (var i = 0; i < count; ++i) {
  137. var guid = window.plugin.waypoints.points[i];
  138. var data = window.plugin.waypoints.pointData[guid];
  139. var point = { "longitude": data.lngE6 / 1E6, "latitude": data.latE6 / 1E6, "name": data.title };
  140.  
  141. points.push(point);
  142. }
  143.  
  144. alert(JSON.stringify(points));
  145. }
  146.  
  147. window.plugin.waypoints.clear = function () {
  148. window.plugin.waypoints.pointData = {};
  149. window.plugin.waypoints.points = [];
  150.  
  151. if (null != window.plugin.waypoints.waypointsLayer) {
  152. // Remove it
  153. window.map.removeLayer(window.plugin.waypoints.waypointsLayer);
  154. window.plugin.waypoints.waypointsLayer = null;
  155. }
  156. }
  157.  
  158. // Setup, is run by IITC at start of the script
  159. var setup = function () {
  160. // Constants
  161. window.plugin.waypoints.CONTROLS_CONTAINER = 'nbdy-waypoints-control-container';
  162. window.plugin.waypoints.CONTROLS_RECORD = 'nbdy-waypoints-control-record';
  163.  
  164. // Variables
  165. window.plugin.waypoints.recording = false;
  166. window.plugin.waypoints.waypointsLayer = null;
  167.  
  168. window.plugin.waypoints.waypointsLayerGroup = new L.LayerGroup();
  169. window.addLayerGroup('Waypoints', window.plugin.waypoints.waypointsLayerGroup, false);
  170.  
  171. // Add the controls
  172. window.map.addControl(new window.plugin.waypoints.controls('bar'));
  173. window.map.on('overlayadd overlayremove', window.plugin.waypoints.toggleControl);
  174. // Make sure its visible, or not.
  175. window.plugin.waypoints.toggleControl();
  176.  
  177. window.plugin.waypoints.clear();
  178.  
  179. // When portals are added to hte map, this hook will capture the click, so it can be used on recording points
  180. window.addHook('portalAdded', window.plugin.waypoints.portalAdded);
  181. };
  182. // PLUGIN END //////////////////////////////////////////////////////////
  183.  
  184. setup.info = plugin_info;
  185. //add the script info data to the function as a property
  186. if (!window.bootPlugins) window.bootPlugins = [
  187. ];
  188. window.bootPlugins.push(setup);
  189. // if IITC has already booted, immediately run the 'setup' function
  190. if (window.iitcLoaded && typeof setup === 'function') setup();
  191. }
  192. // wrapper end
  193. // inject code into site context
  194.  
  195. var script = document.createElement('script');
  196. var info = {};
  197. if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = {
  198. version: GM_info.script.version,
  199. name: GM_info.script.name,
  200. description: GM_info.script.description
  201. };
  202. script.appendChild(document.createTextNode('(' + wrapper + ')(' + JSON.stringify(info) + ');'));
  203. (document.body || document.head || document.documentElement).appendChild(script);
Add Comment
Please, Sign In to add comment