Skip to content

Commit

Permalink
added waypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
abarnhard committed Aug 23, 2014
1 parent e5cf697 commit 2ec647d
Show file tree
Hide file tree
Showing 6 changed files with 1,189 additions and 19 deletions.
3 changes: 2 additions & 1 deletion app/controllers/stops.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
var Stop = require('../models/stop');

exports.create = function(req, res){
console.log(req.body);
Stop.create(req.body, function(){
res.redirect('/treasures/' + req.params.tripId);
res.redirect('/trips/' + req.params.tripId);
});
};

Expand Down
20 changes: 17 additions & 3 deletions app/models/stop.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var Mongo = require('mongodb');
var Mongo = require('mongodb'),
async = require('async');

function Stop(o){
this.loc = {name:o.name, lat:parseFloat(o.lat), lng:parseFloat(o.lng)};
Expand All @@ -22,8 +23,21 @@ Stop.find = function(id, cb){
};

Stop.create = function(obj, cb){
var s = new Stop(obj);
Stop.collection.save(s, cb);
var stops = Object.keys(obj).map(function(k){
return obj[k];
});
async.map(stops, function(o, done){
var s = new Stop(o);
s.save(function(){
done(null, s);
});
}, function(err, savedStops){
cb();
});
};

Stop.prototype.save = function(cb){
Stop.collection.save(this, cb);
};

module.exports = Stop;
Expand Down
6 changes: 3 additions & 3 deletions app/static/js/user/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function cartographer(cssId, lat, lng, zoom){

function calcRoute(waypoints, cb){
'use strict';
console.log('waypoints', waypoints);
// console.log('waypoints', waypoints);
var directionsService = new google.maps.DirectionsService(),
origin = waypoints[0],
destination = waypoints[waypoints.length - 1];
Expand All @@ -36,10 +36,10 @@ function calcRoute(waypoints, cb){
origin:origin,
destination:destination,
waypoints:waypoints,
optimizeWaypoints:true,
optimizeWaypoints:false,
travelMode:google.maps.TravelMode.DRIVING
};
console.log('request', request);
// console.log('request', request);
directionsService.route(request, function(response, status){
if(status === google.maps.DirectionsStatus.OK){
//console.log('response', response);
Expand Down
46 changes: 37 additions & 9 deletions app/static/js/user/trips-show.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global cartographer, google, calcRoute */
/* global cartographer, google, calcRoute, async, geocode */

(function(){
'use strict';
Expand All @@ -8,6 +8,7 @@

$(document).ready(function(){
$('button[type=button]').click(addStop);
$('button[type=submit]').click(createStops);
map = cartographer('trip-map', 39.8282, -98.5795, 4);

var directionsDisplay = new google.maps.DirectionsRenderer(),
Expand All @@ -22,16 +23,43 @@

});

function createStops(e){
debugger;
var stopGroups = $('.stop-group').toArray();
console.log(stopGroups);
async.map(stopGroups, function(stopGroup, done){
var locName = $(stopGroup).children('input[data-id=name]').val();
geocode(locName, function(name, lat, lng){
$(stopGroup).children('input[data-id=name]').val(name);
$(stopGroup).children('input[data-id=lat]').val(lat);
$(stopGroup).children('input[data-id=lng]').val(lng);
done(null, stopGroup);
});
}, function(err, geocodedDivs){
console.log(geocodedDivs);
$('form').submit();
});
e.preventDefault();
}

function addStop(){
var $last = $('form > .stop-group:last-of-type'),
$clone = $last.clone(),
$i = $clone.children('input');
count++;
$i.val('');
$i.attr('placeholder', '');
$i.attr('name', 'stops['+count+']');
$last.after($clone);
$i.focus();
var $last = $('form > .stop-group:last-of-type'),
$formGroup = $('<div>'),
name = 'stop' + count,
$i;

$formGroup.addClass('form-group').addClass('stop-group');
$i = $('<input>').prop('type', 'text').prop('name', name + '[name]').attr('data-id', 'name').addClass('form-control');
$formGroup.append($i);
$i = $('<input>').prop('type', 'hidden').prop('name', name + '[lat]').attr('data-id', 'lat');
$formGroup.append($i);
$i = $('<input>').prop('type', 'hidden').prop('name', name + '[lng]').attr('data-id', 'lng');
$formGroup.append($i);
$i = $('<input>').prop('type', 'hidden').prop('name', name + '[tripId]').attr('data-id', 'tripId').val($('form').attr('data-trip-id'));
$formGroup.append($i);
$last.after($formGroup);
$formGroup.children('input[type=text]').focus();
}

function makeWaypoints(){
Expand Down
Loading

0 comments on commit 2ec647d

Please sign in to comment.