Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name mapMarkers
- // @namespace mapMarkers
- // @include https://www.mydomain.com/*[1]
- // @description map markers of addresses in table
- // @version 1
- // @grant none
- // ==/UserScript==
- // find the table and loop through each rows to get the 11th, 12th, 13th cell's content (street address, city and zip respectively
- // convert to lat/lon and show markers on map
- if (document.getElementById('main_report') != null) {
- var script = document.createElement('script');
- script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&callback=initialize';
- (document.head || document.body).appendChild(script);
- var Table_1 = document.getElementById('main_report');
- var DIVmap = document.createElement('div');
- DIVmap.id = 'DIVmap';
- DIVmap.style.border = '2px coral solid';
- DIVmap.style.height = DIVmap.style.width = '35%';
- DIVmap.style.margin = DIVmap.style.padding = '1';
- DIVmap.style.position = 'fixed';
- DIVmap.style.right = DIVmap.style.bottom = '1%';
- DIVmap.style.zIndex = '999';
- var DIVinternal = document.createElement('div');
- DIVinternal.id = 'DIVinternal';
- DIVinternal.style.height = DIVinternal.style.width = '100%';
- document.body.appendChild(DIVmap);
- DIVmap.appendChild(DIVinternal);
- //Adds a button which allows the user to re-run calcRoute
- var reloadMapButton = document.createElement("button");
- reloadMapButton.setAttribute("type", "button");
- reloadMapButton.textContent="Reload map";
- reloadMapButton.id="calcRoute";
- reloadMapButton.style.zIndex = '1000';
- document.getElementById('Content_Title').appendChild(reloadMapButton);
- reloadMapButton.onclick = calcRoute;
- window.initialize = function () {
- var google = window.google,
- directionsService = new google.maps.DirectionsService(),
- directionsDisplay = new google.maps.DirectionsRenderer(),
- myLoc = new google.maps.LatLng(28.882193,-81.317936),
- myOptions = {
- zoom: 13,
- mapTypeId: google.maps.MapTypeId.ROADMAP,
- center: myLoc
- },
- map = new google.maps.Map(document.getElementById("DIVinternal"), myOptions),
- geocoder = new google.maps.Geocoder();
- function calcRoute() {
- for (var i = 1, row; row = Table_1.rows[i]; i++) {
- address = row.cells[10].title + ', ' + row.cells[11].innerHTML + ', ' + row.cells[12].innerHTML,
- name = row.cells[10].title + ', ' + row.cells[11].innerHTML + ', ' + row.cells[12].innerHTML;
- setTimeout( function () { // timer to avoid google geocode limits
- geocoder.geocode( { 'address': address}, function(results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- var marker = new google.maps.Marker({
- map: map,
- position: results[0].geometry.location,
- title: name
- }),
- infowindow = new google.maps.InfoWindow({
- content: name
- });
- google.maps.event.addListener(marker, 'click', function() {
- infowindow.open(map,marker);
- });
- } else {
- console.error("Geocode was not successful for the following reason: " + status);
- }
- });
- }, i * 350);
- }
- }
- document.getElementById("calcRoute").onclick = calcRoute
- calcRoute();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement