Advertisement
Guest User

Untitled

a guest
Jun 26th, 2015
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2.  
  3. // @name mapMarkers
  4.  
  5. // @namespace mapMarkers
  6.  
  7. // @include https://www.mydomain.com/*[1]
  8.  
  9. // @description map markers of addresses in table
  10.  
  11. // @version 1
  12.  
  13. // @grant none
  14.  
  15. // ==/UserScript==
  16.  
  17. // find the table and loop through each rows to get the 11th, 12th, 13th cell's content (street address, city and zip respectively
  18.  
  19. // convert to lat/lon and show markers on map
  20.  
  21. if (document.getElementById('main_report') != null) {
  22.  
  23.     var script = document.createElement('script');
  24.         script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&callback=initialize';
  25.  
  26.         (document.head || document.body).appendChild(script);
  27.  
  28.  
  29.     var Table_1 = document.getElementById('main_report');
  30.  
  31.     var DIVmap = document.createElement('div');
  32.         DIVmap.id = 'DIVmap';
  33.         DIVmap.style.border = '2px coral solid';
  34.         DIVmap.style.height = DIVmap.style.width = '35%';
  35.         DIVmap.style.margin = DIVmap.style.padding = '1';
  36.         DIVmap.style.position = 'fixed';
  37.         DIVmap.style.right = DIVmap.style.bottom = '1%';
  38.         DIVmap.style.zIndex = '999';
  39.  
  40.     var DIVinternal = document.createElement('div');
  41.         DIVinternal.id = 'DIVinternal';
  42.         DIVinternal.style.height = DIVinternal.style.width = '100%';
  43.  
  44.         document.body.appendChild(DIVmap);
  45.         DIVmap.appendChild(DIVinternal);
  46.  
  47.     //Adds a button which allows the user to re-run calcRoute
  48.     var reloadMapButton = document.createElement("button");
  49.         reloadMapButton.setAttribute("type", "button");
  50.         reloadMapButton.textContent="Reload map";
  51.         reloadMapButton.id="calcRoute";
  52.         reloadMapButton.style.zIndex = '1000';
  53.         document.getElementById('Content_Title').appendChild(reloadMapButton);
  54.         reloadMapButton.onclick = calcRoute;
  55.  
  56.     window.initialize = function () {
  57.  
  58.         var google = window.google,
  59.             directionsService = new google.maps.DirectionsService(),
  60.             directionsDisplay = new google.maps.DirectionsRenderer(),
  61.             myLoc = new google.maps.LatLng(28.882193,-81.317936),
  62.             myOptions = {
  63.                 zoom: 13,
  64.                 mapTypeId: google.maps.MapTypeId.ROADMAP,
  65.                 center: myLoc
  66.             },
  67.             map = new google.maps.Map(document.getElementById("DIVinternal"), myOptions),
  68.             geocoder = new google.maps.Geocoder();
  69.  
  70.         function calcRoute() {
  71.             for (var i = 1, row; row = Table_1.rows[i]; i++) {
  72.                 address = row.cells[10].title +  ', ' + row.cells[11].innerHTML + ', ' + row.cells[12].innerHTML,
  73.                 name = row.cells[10].title +  ', ' + row.cells[11].innerHTML + ', ' + row.cells[12].innerHTML;
  74.            
  75.                 setTimeout( function () {  // timer to avoid google geocode limits
  76.                     geocoder.geocode( { 'address': address}, function(results, status) {
  77.                         if (status == google.maps.GeocoderStatus.OK) {
  78.                             var marker = new google.maps.Marker({
  79.                                     map: map,
  80.                                     position: results[0].geometry.location,
  81.                                     title: name                            
  82.                                 }),
  83.                                 infowindow = new google.maps.InfoWindow({
  84.                                     content: name
  85.                                 });
  86.  
  87.                             google.maps.event.addListener(marker, 'click', function() {
  88.                                 infowindow.open(map,marker);
  89.                             });                        
  90.                         } else {
  91.                             console.error("Geocode was not successful for the following reason: " + status);
  92.                         }
  93.                     });
  94.                 }, i * 350);
  95.             }
  96.         }
  97.  
  98.         document.getElementById("calcRoute").onclick = calcRoute
  99.         calcRoute();
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement