This commit is contained in:
Daniel Camargo 2013-02-28 16:41:13 -03:00
parent 911a8cbef8
commit 65f9ff2b51
3 changed files with 65 additions and 6 deletions

View File

@ -36,6 +36,7 @@ var App = {
_init: false, _init: false,
_pageInit: false, _pageInit: false,
_identified: false, _identified: false,
isDeliveryAddressOk : false,
tips: [0,5,10,15,20,25] tips: [0,5,10,15,20,25]
}; };
@ -775,6 +776,27 @@ Issue 13: Removed the password for while
return; return;
} }
// Check the distance between the user and the restaurant
if( order.delivery_type == 'delivery' && !App.isDeliveryAddressOk ){
App.loc.geocodeDelivery( order.address,
function(){
if( App.isDeliveryAddressOk ){
if( !App.restaurant.deliveryHere( { lat : App.loc.lat, lon : App.loc.lon } )){
alert( 'Sorry, you are too far from this restaurant!' );
App.isDeliveryAddressOk = false;
App.busy.unBusy();
return;
} else {
App.busy.unBusy();
App.cart.submit();
}
} else {
App.busy.unBusy();
}
} );
return;
}
$.ajax({ $.ajax({
url: App.service + 'order', url: App.service + 'order',
data: order, data: order,
@ -1015,7 +1037,7 @@ App.test = {
$('[name="pay-name"]').val('MR TEST'); $('[name="pay-name"]').val('MR TEST');
$('[name="pay-phone"]').val('***REMOVED***'); $('[name="pay-phone"]').val('***REMOVED***');
$('[name="pay-address"]').val("123 main\nsanta monica ca"); $('[name="pay-address"]').val( App.restaurant.address || "123 main\nsanta monica ca" );
App.order.cardChanged = true; App.order.cardChanged = true;
}, },
@ -1048,17 +1070,16 @@ App.loc = {
distance: function(params) { distance: function(params) {
try{ try{
var R = 6371; // Radius of the earth in km var R = 6371; // Radius of the earth in km
var dLat = (params.to.lat - params.from.lat).toRad(); var dLat = _toRad(params.to.lat - params.from.lat);
var dLon = _toRad(params.to.lon - params.from.lon);
var dLon = (params.to.lon - params.from.lon).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(params.from.lat.toRad()) * Math.cos(params.to.lat.toRad()) * Math.cos(_toRad(params.from.lat)) * Math.cos(_toRad(params.to.lat)) *
Math.sin(dLon/2) * Math.sin(dLon/2); Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km var d = R * c; // Distance in km
return d; return d;
} catch( e ) { } catch( e ) {
console.log( 'error', e );
App.track('Location Error', { App.track('Location Error', {
lat: App.loc.lat, lat: App.loc.lat,
lon: App.loc.lon, lon: App.loc.lon,
@ -1251,6 +1272,24 @@ App.loc = {
complete(); complete();
}); });
}, },
geocodeDelivery : function( address, complete ){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
App.loc.lat = results[0].geometry.location.lat();
App.loc.lon = results[0].geometry.location.lng();
App.isDeliveryAddressOk = true;
App.registerLocationsCookies();
} else {
alert( 'Oops! We couldn\'t find that address!' );
App.isDeliveryAddressOk = false;
}
complete();
});
},
reverseGeocode: function(complete) { reverseGeocode: function(complete) {
App.track('Location Reverse Geocode', { App.track('Location Reverse Geocode', {
lat: App.loc.lat, lat: App.loc.lat,
@ -1288,6 +1327,12 @@ App.loc = {
} }
}); });
},
km2Miles : function( km ){
return km * 0.621371;
},
Miles2Km : function( miles ){
return miles * 1.60934;
} }
} }
@ -1516,10 +1561,12 @@ $(function() {
}); });
$(document).on('click', '.button-submitorder', function() { $(document).on('click', '.button-submitorder', function() {
App.isDeliveryAddressOk = false;
App.cart.submit($(this)); App.cart.submit($(this));
}); });
$(document).on('click', '.button-submitorder-form', function() { $(document).on('click', '.button-submitorder-form', function() {
App.isDeliveryAddressOk = false;
App.cart.submit($(this),true); App.cart.submit($(this),true);
}); });

View File

@ -10,6 +10,12 @@ if (typeof(Number.prototype.toRad) === 'undefined') {
} }
} }
// Sometimes jQuery doesn't understand the Number.toRad()
function _toRad(Value) {
/** Converts numeric degrees to radians */
return Value * Math.PI / 180;
}
var History = window.History; var History = window.History;
History.Adapter.bind(window,'statechange',function() { History.Adapter.bind(window,'statechange',function() {

View File

@ -196,6 +196,12 @@ var Restaurant = function(id) {
return isOpen; return isOpen;
} }
self.deliveryHere = function( param ){
var km = App.loc.distance( { from : { lat : param.lat, lon : param.lon }, to : { lat : this.loc_lat, lon : this.loc_long } } );
var miles = App.loc.km2Miles( km );
return ( miles <= this.delivery_radius )
}
self.preset = function() { self.preset = function() {
return self['_preset']; return self['_preset'];
} }