Created
December 6, 2015 05:39
-
-
Save xydrolase/8d6eb9ba08df1ef8813a to your computer and use it in GitHub Desktop.
Overlay MBTA Routes to ApartmentGuide
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name MBTA Routes Renderer | |
| // @namespace http://tampermonkey.net/ | |
| // @version 0.1 | |
| // @description try to take over the world! | |
| // @author You | |
| // @match http://www.apartmentguide.com/map* | |
| // @grant GM_xmlhttpRequest | |
| // @require http://code.jquery.com/jquery-latest.js | |
| // ==/UserScript== | |
| /* jshint -W097 */ | |
| 'use strict'; | |
| // component/modules defined by AptGuides | |
| var base_map = null; | |
| var markers = null; | |
| function renderSubway(data) { | |
| var stops = data.Stop; | |
| var pts = []; | |
| for (var i = 0; i < stops.length; ++i) { | |
| var stop = data.Stop[i]; | |
| pts.push(new google.maps.LatLng(parseFloat(stop.Lat), parseFloat(stop.Long))); | |
| } | |
| var route_path = new google.maps.Polyline({ | |
| path: pts, | |
| strokeColor: data.lines[0].colour, | |
| strokeWeight: 4, | |
| strokeOpacity: 0.8 | |
| }); | |
| route_path.setMap(base_map.attr.gMap); | |
| } | |
| function initialize() { | |
| var registry = require("flight/lib/registry"); | |
| var map_node = document.getElementById("map_canvas"); | |
| registry.components.forEach(function(comp) { | |
| // find ui/base_map | |
| if (comp.isAttachedTo(map_node)) { | |
| Object.keys(comp.instances).forEach( | |
| function(key) { | |
| if (comp.instances[key].instance.attr.hasOwnProperty('gMap')) { | |
| base_map = comp.instances[key].instance; | |
| } | |
| }); | |
| } | |
| // try to find ui/markers | |
| Object.keys(comp.instances).forEach( | |
| function(key) { | |
| if (comp.instances[key].instance.attr.hasOwnProperty('markers')) { | |
| markers = comp.instances[key].instance; | |
| } | |
| }); | |
| }); | |
| var schepens = new google.maps.LatLng(42.3620519,-71.0664637); | |
| var m_work = new google.maps.Marker({ | |
| position: schepens, | |
| title: 'Schepends Eye Research Insitute', | |
| icon: 'http://maps.google.com/mapfiles/arrow.png' | |
| }); | |
| m_work.setMap(base_map.attr.gMap); | |
| var routes = [["subway", "blue", "BLUE", "/lib/json/blueline.json"], ["subway", "orange", "ORANGE", "/lib/json/orangeline.json"], ["subway", "red", "RED", "/lib/json/redline_braintree.json"], ["subway", "red", "RED", "/lib/json/redline_ashmont.json"], ["subway", "red", "MATTAPAN", "/lib/json/redline_mattapan.json"], ["subway", "green", "GREEN-B", "/lib/json/greenlineb.json"], ["subway", "green", "GREEN-C", "/lib/json/greenlinec.json"], ["subway", "green", "GREEN-D", "/lib/json/greenlined.json"], ["subway", "green", "GREEN-E", "/lib/json/greenlinee.json"], ["subway", "silver", "SL1", "/lib/json/sl1.json"], ["subway", "silver", "SL2", "/lib/json/sl2.json"], ["subway", "silver", "SL4", "/lib/json/sl4.json"], ["subway", "silver", "SL5", "/lib/json/sl5.json"], ["rail", "attleborostoughton", "PROVSTOU", "/lib/json/cr_attleboro.json"], ["rail", "attleborostoughton", "PROVSTOU", "/lib/json/cr_stoughton.json"], ["rail", "fairmont", "FAIRMNT", "/lib/json/cr_fairmont.json"], ["rail", "fitchburg", "FITCHBRG", "/lib/json/cr_fitchburg.json"], ["rail", "framinghamworcester", "WORCSTER", "/lib/json/cr_framinghamworcester.json"], ["rail", "franklin", "FRANKLIN", "/lib/json/cr_franklin.json"], ["rail", "greenbush", "GREENBSH", "/lib/json/cr_greenbush.json"], ["rail", "haverhillreading", "HAVRHILL", "/lib/json/cr_haverhillreading.json"], ["rail", "lowell", "LOWELL", "/lib/json/cr_lowell.json"], ["rail", "middleboroughlakeville", "MIDLBORO", "/lib/json/cr_middleboroughlakeville.json"], ["rail", "needham", "NEEDHAM", "/lib/json/cr_needham.json"], ["rail", "newburyportrockport", "NBRYROCK", "/lib/json/cr_newburyport.json"], ["rail", "newburyportrockport", "NBRYROCK", "/lib/json/cr_rockport.json"], ["rail", "oldcolony", "OLCOLONY", "/lib/json/cr_oldcolony.json"], ["rail", "plymouthkingston", "KINGSTON", "/lib/json/cr_plymouthkingston.json"], ["boats", "F1", "F1", "/lib/json/ferry_f1.json"], ["boats", "F2", "F2", "/lib/json/ferry_f2.json"], ["boats", "F2H-BOS", "F2H-BOS", "/lib/json/ferry_f2hboston.json"], ["boats", "F2HLOGAN", "F2HLOGAN", "/lib/json/ferry_f2hlogan.json"], ["boats", "F2H", "F2H", "/lib/json/ferry_f2hlogan.json"], ["boats", "F4", "F4", "/lib/json/ferry_f4.json"]]; | |
| for (var rid = 0; rid < routes.length; ++rid) { | |
| var r = routes[rid]; | |
| if (r[0] != "subway") continue; | |
| setTimeout((function(r) { | |
| if (r[0] != "subway") return; | |
| var json_uri = r[3]; | |
| GM_xmlhttpRequest ({ | |
| method: "GET", | |
| url: "http://www.mbta.com" + json_uri, | |
| headers: { | |
| "Content-Type": "application/json" | |
| }, | |
| onload: function (response) { | |
| var data = JSON.parse(response.responseText); | |
| renderSubway(data); | |
| } | |
| }); | |
| })(r), 500 * rid); | |
| } | |
| } | |
| $(document).ready(function() { | |
| setTimeout(initialize, 7500); | |
| /* | |
| */ | |
| }); | |
| // by injecting code into markers.addMarkers, we can inject any fake listing | |
| // data, or call our customized renderer. | |
| // test the Google Map object | |
| /* | |
| var loc_fenway = new google.maps.LatLng(42.3464, -71.0975); | |
| var m_fenway = new google.maps.Marker({ | |
| map: base_map.attr.gMap, | |
| icon: "http://www.apartmentguide.com/assets/nonsprite/map/map_pin_red4-fded538e.png", | |
| position: loc_fenway, | |
| title: "Fenway Park!!!!", | |
| visible: true | |
| }); | |
| var l_fenway = { | |
| free: false, | |
| id: "10000111", | |
| }; | |
| */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment