Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapService module now loads all geojson files and poll data #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -187,7 +187,6 @@ require(['jquery',
var link = $('<a>').text(result.formatted_address).data('location', result.geometry.location).on('click', addressClickHandler);
$('<li>').append(link).appendTo($ul);
}
$('.modal').modal('hide');
}
}

Expand Down
16 changes: 7 additions & 9 deletions app/scripts/early_voting_mgr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
}
}
}

Expand All @@ -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
}));

Expand Down
56 changes: 40 additions & 16 deletions app/scripts/map_service.js
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
Expand All @@ -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<len; i++){

var currentFeature = precincts[i],
currentPolygon = new google.maps.Polygon({
paths: currentFeature.getGeometry().getAt(0).getArray(),
clickable: false
});

polygons.push(currentPolygon);
}

return polygons;
}

function clearUserInputs() {

userInputs.precinct = null;
Expand Down Expand Up @@ -182,10 +206,10 @@ define(['json!vendor/EARLY_VOTING_AddressPoints.geojson'],
displayDirections(latLng, destination, successCallback, errorCallback);

},
googleMap : map,
precinctsDataLayer : precinctsDataLayer,
earlyPollsDataLayer : earlyPollsDataLayer,
electionPollsDataLayer : electionPollsDataLayer
};
googleMap : map,
precincts : precincts,
precinctsPolygons : precinctsPolygons,
pollingLocations : pollingLocations,
earlyPollingLocations : earlyPollingLocations,
};
});
52 changes: 14 additions & 38 deletions app/scripts/polling_location_finder.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,34 @@
define(['jquery',
'json!vendor/ELECTIONS_WardsPrecincts.geojson',
'json!vendor/ELECTIONS_PollingLocations.geojson',
'map_service'],
function($, precinctsJSON, locationsJSON, mapService) {
function($, mapService) {

'use strict';

var precincts = mapService.earlyPollsDataLayer.addGeoJson(precinctsJSON),
pollingLocations = mapService.electionPollsDataLayer.addGeoJson(locationsJSON),
precinctsPolygons = [];

createPolygons();

//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;

for(i; i<len; i++){

var currentFeature = precincts[i],
currentPolygon = new google.maps.Polygon({
paths: currentFeature.getGeometry().getAt(0).getArray(),
clickable: false
});

precinctsPolygons.push(currentPolygon);

}
}


function getUserPrecinct(latLng) {

for (var i = 0, len1 = precinctsPolygons.length; i < len1; i++) {
if (precinctsPolygons[i].containsLatLng(latLng)) {
return precinctsPolygons[i];
for (var i = 0, len1 = mapService.precinctsPolygons.length; i < len1; i++) {
var currentPrecinct = mapService.precinctsPolygons[i];

if (currentPrecinct.containsLatLng(latLng)) {
return currentPrecinct;
}

}

}

function getPollingLocation(precinct) {

var index = precinctsPolygons.indexOf(precinct);
var index = mapService.precinctsPolygons.indexOf(precinct);
// find out which ward/precinct they're in using Point in Polygon
var wardPrecinct = precincts[index].getProperty('WardPrecinct');
var wardPrecinct = mapService.precincts[index].getProperty('WardPrecinct');
if (wardPrecinct === "3-2A") {
wardPrecinct = "3-2";
}
//Search for the polling location that matches the precinct and ward
for (var j = 0, len2 = pollingLocations.length; j < len2; j++) {
if (pollingLocations[j].getProperty('W_P') === wardPrecinct) {
return pollingLocations[j];
for (var j = 0, len2 = mapService.pollingLocations.length; j < len2; j++) {
if (mapService.pollingLocations[j].getProperty('W_P') === wardPrecinct) {
return mapService.pollingLocations[j];
}
}
}
Expand All @@ -67,7 +43,7 @@ define(['jquery',
return encodeURI(url + destination);
}


return function(latLng, successCallback, errorCallback) {


Expand Down