/*! * jQuery Map Calculator - A Map Distance calculator for Lee Sadler * * * @author : Serdar Sanri * @doc : http://serdarsanri.com * @version : 1.0.0 * */ ; (function($) { 'use strict'; var oMapCalculator = function(){} oMapCalculator.prototype = { map : null, origin : null, directionsService : null, directiondisplay : null, directionsdisplays : [], polylines : [], totals : {}, defaults : { base : [51.345142, 0.009356], zoom : 10, currency : '', target : '', map : '', button : "", total_price_only : '', total_price_wo_vat : "", total_distance : "", api_key : '', vat : 20 }, options : {}, init : function(el, options){ var obj = this; this.options = $.extend({}, this.defaults, options, $(el).data() ); this.$el = $(el); this.$target = $(this.options.target); this.$map = $(this.options.map); this.$result = $(this.options.result); if(typeof google == "undefined") $.getScript("//maps.googleapis.com/maps/api/js?key=" + this.options.api_key , function(){ obj.initMap(); }); else obj.initMap(); }, initMap : function(){ var obj = this; this.origin = new google.maps.LatLng(this.options.base[0], this.options.base[1]); this.map = new google.maps.Map( $(this.$map)[0] , { zoom: this.options.zoom, center: this.origin, }); $(document.body).on("click" ,this.options.button , function(){ obj.calculateAndDisplayRoute(obj); }); }, calculateAndDisplayRoute : function(obj) { if( obj.directiondisplay!=null ){ obj.directiondisplay.setMap(null); obj.directiondisplay = null; } obj.directiondisplay = new google.maps.DirectionsRenderer; obj.directionsService = new google.maps.DirectionsService; obj.directionsService.route({ origin: obj.origin, destination: obj.$target.val(), travelMode: google.maps.TravelMode.DRIVING, provideRouteAlternatives: true, optimizeWaypoints:true, }, function(response, status) { if (status === google.maps.DirectionsStatus.OK) { obj.directiondisplay.setDirections(response); obj.directiondisplay.setMap(obj.map); obj.renderDirectionsPolylines(response); } else { window.alert('Directions request failed due to ' + status); } }); }, renderDirectionsPolylines : function(response) { var obj = this; obj.polylines = []; obj.totals = {duration : 0, distance : 0 , price : 0}; var legs = response.routes[0].legs; for (var i = 0; i < legs.length; i++) { var steps = legs[i].steps; for (var j = 0; j < steps.length; j++) { obj.totals.distance += (steps[j].distance.value) * 0.000621371; obj.totals.duration += steps[j].duration.value; } } if( obj.totals.distance <= 3 ) obj.totals.price = 5; if( obj.totals.distance >3 ) obj.totals.price = 5 + obj.totals.price + ( ( obj.totals.distance - 3 ) * 1.5 ); $( obj.options.total_price_only).html(obj.options.currency + "" + ( obj.totals.price * ( obj.options.vat +100)/100 ).toFixed(2) ); $( obj.options.total_price_wo_vat).html(obj.options.currency + "" + ( obj.totals.price ).toFixed(2) + " + <small>%"+obj.options.vat+" VAT</small>" ); $( obj.options.total_distance).html( obj.totals.distance.toFixed(2)+ " miles"); } } $.fn.MapCalculator = function(options){ return this.each( function(){ if( typeof $(this).data('MapCalculator')=="undefined" ) { var mp = new oMapCalculator(); mp.init(this , options); $(this).data('MapCalculator' , mp); return mp; } return $(this).data('MapCalculator'); } ); } })(jQuery);