From 3f94b0d93ffd6aaae4548e87e91c4fcb515ddea2 Mon Sep 17 00:00:00 2001 From: moczka Date: Thu, 27 Oct 2016 13:36:04 -0400 Subject: [PATCH] Improving map service module by making it the sole responsible of all the map rendering and functionality --- app/scripts/app.js | 11 +++-- app/scripts/early_voting_mgr.js | 16 ++++---- app/scripts/map_service.js | 56 ++++++++++++++++++-------- app/scripts/polling_location_finder.js | 52 +++++++----------------- 4 files changed, 66 insertions(+), 69 deletions(-) diff --git a/app/scripts/app.js b/app/scripts/app.js index 341d025..b1703aa 100644 --- a/app/scripts/app.js +++ b/app/scripts/app.js @@ -69,12 +69,12 @@ require.config({ } }); - - require(['jquery', - 'early_voting_mgr', 'polling_location_finder', 'map_service', - 'json!vendor/EARLY_VOTING_AddressPoints.geojson'], - function($, earlyVotingManager, findPollingLocationFor, mapService, earlyPollingJSON) { + 'map_service', + 'polling_location_finder', + 'early_voting_mgr'], + function($, mapService, findPollingLocationFor, earlyVotingManager) { + 'use strict'; window.location.hash = window.location.hash || 'early-voting'; @@ -187,7 +187,6 @@ require(['jquery', var link = $('').text(result.formatted_address).data('location', result.geometry.location).on('click', addressClickHandler); $('
  • ').append(link).appendTo($ul); } - $('.modal').modal('hide'); } } diff --git a/app/scripts/early_voting_mgr.js b/app/scripts/early_voting_mgr.js index eb13365..6a2cdbc 100644 --- a/app/scripts/early_voting_mgr.js +++ b/app/scripts/early_voting_mgr.js @@ -2,14 +2,11 @@ define( [ 'jquery', 'moment', 'ejs', 'map_service', - 'json!vendor/EARLY_VOTING_AddressPoints.geojson', 'text!templates/early_voting_sidebar.ejs', 'scrollTo', 'moment_range', 'bootstrapCollapse' ], - function($, moment, ejs, mapService, earlyVotingJSON, earlyVotingSidebarTmpl) { + function($, moment, ejs, mapService, earlyVotingSidebarTmpl) { 'use strict'; - - var earlyVotingLocations = mapService.earlyPollsDataLayer.addGeoJson(earlyVotingJSON); - + var $el = $('#early-voting'); function getDirections(destination) { @@ -24,11 +21,12 @@ define( function whenMarkerEventsHappen(eventType, marker) { if (eventType === 'click') { - for (var i = 0; i < earlyVotingLocations.length; i++) { - if (marker.getPosition().equals(earlyVotingLocations[i].getGeometry().get())) { + for (var i = 0; i < mapService.earlyPollingLocations.length; i++) { + var currentEarlyPoll = mapService.earlyPollingLocations[i].getGeometry().get(); + if (marker.getPosition().equals(currentEarlyPoll)) { $el.scrollTo($('#location'+i), 800); } - } + } } } @@ -51,7 +49,7 @@ define( init: function() { $el.find('#early-voting-sidebar').html(ejs.render(earlyVotingSidebarTmpl, { moment: moment, - locations: earlyVotingLocations, + locations: mapService.earlyPollingLocations, getDirections: getDirections })); diff --git a/app/scripts/map_service.js b/app/scripts/map_service.js index b6c14f5..1cd5e67 100644 --- a/app/scripts/map_service.js +++ b/app/scripts/map_service.js @@ -1,10 +1,11 @@ -define(['json!vendor/EARLY_VOTING_AddressPoints.geojson'], - function(earlyPollingJSON) { +define(['json!vendor/EARLY_VOTING_AddressPoints.geojson', + 'json!vendor/ELECTIONS_WardsPrecincts.geojson', + 'json!vendor/ELECTIONS_PollingLocations.geojson'], + function(earlyPollingJSON, precinctsJSON, locationsJSON) { var hoverIcon = "https://mts.googleapis.com/maps/vt/icon/name=icons/spotlight/spotlight-waypoint-a.png&text=•&psize=30&font=fonts/Roboto-Regular.ttf&color=ff333333&ax=44&ay=48&scale=1" var defaultIcon = "https://mts.googleapis.com/maps/vt/icon/name=icons/spotlight/spotlight-waypoint-b.png&text=•&psize=30&font=fonts/Roboto-Regular.ttf&color=ff333333&ax=44&ay=48&scale=1" - - + var DEFAULT_ZOOM_LEVEL = 13; var DEFAULT_CENTER_POSITION = new google.maps.LatLng(42.3736, -71.1106); // Cambridge @@ -16,8 +17,12 @@ define(['json!vendor/EARLY_VOTING_AddressPoints.geojson'], var earlyPollsDataLayer = new google.maps.Data(), precinctsDataLayer = new google.maps.Data(), electionPollsDataLayer = new google.maps.Data(), - earlyPollingLocations = earlyPollsDataLayer.addGeoJson(earlyPollingJSON); - + precincts = earlyPollsDataLayer.addGeoJson(precinctsJSON), + pollingLocations = electionPollsDataLayer.addGeoJson(locationsJSON), + earlyPollingLocations = earlyPollsDataLayer.addGeoJson(earlyPollingJSON), + precinctsPolygons = createPolygons(); + + var directionsService = new google.maps.DirectionsService(), directionsDisplay = new google.maps.DirectionsRenderer({ map: map, @@ -31,12 +36,31 @@ define(['json!vendor/EARLY_VOTING_AddressPoints.geojson'], "precinct": null, "homeAddress": null, "destination": null - } - + }; var earlyPollingMarkers = []; - - + +//function that populates the array with polygons representing each precinct, because data.polygon has little to no useful methods. + function createPolygons(){ + + var i = 0, + len = precincts.length, + polygons = []; + + for(i; i