function displayIntoDiv(divName, info) { $('#' + divName).html(info); } function roundNumber(num, dec) { var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); return result; } function convert_meters(distance){ //Converts meters to miles var miles = distance * 0.000621371192; miles = roundNumber(miles,2); //converts meters to km var km = distance / 1000; km = roundNumber(km,2); //export distances in array var distance_array = []; distance_array[0] = miles; distance_array[1] = km; return distance_array; } function Coordinates(){ //Gathers info from page this.clickPass = function(){ var glocation; var glocation2; glocation = $('#user_address1').val(); glocation2 = $('#user_address2').val(); //Dumps user input into div displayIntoDiv('location1', glocation); displayIntoDiv('location2', glocation2); //Enters user input into newDrive object this.inputLocations = []; this.inputLocations[0] = glocation; this.inputLocations[1] = glocation2; }; //holds drive distance this.driveDistance = 0; } function googleDriveCall(newDrive){ //Render object var directionDisplay; //Directions object var directionsService = new google.maps.DirectionsService(); //Setup request using user input var gOrigin = newDrive.inputLocations[0]; var gFinal = newDrive.inputLocations[1]; //Create Direction Request object var gReq = { origin: gOrigin, destination: gFinal, travelMode: google.maps.DirectionsTravelMode.DRIVING }; //Call Directions Service and use callback for the rest of the function directionsService.route(gReq, function(results, status){ if (status == google.maps.DirectionsStatus.OK) { //console.log(results); $('div#error').empty(); var google_d = results.routes[0].legs[0].distance.value; google_d = convert_meters(google_d) //newDrive.driveDistance = google_d[0] + "mi / " + google_d[1] + "km"; newDrive.driveDistance = google_d[1] + "km"; displayIntoDiv('location1', results.routes[0].legs[0].start_address); displayIntoDiv('location2', results.routes[0].legs[0].end_address); displayIntoDiv('drive_distance', newDrive.driveDistance); directionsDisplay = new google.maps.DirectionsRenderer(); directionsDisplay.setDirections(results); showMap(directionsDisplay); } else { //console.log(results); var error_message = 'Ne mogu da izračunam rastojanje izmedju dva mesta. Ili niste upisali u polja gradove?'; displayIntoDiv('error', error_message); $('div#map_canvas').fadeOut(); $('div.answers').fadeOut(); $('div#error').fadeIn(); } }); } function showMap(directionsDisplay) { $('div.answers').fadeIn(); var chicago = new google.maps.LatLng(41.850033, -87.6500523); var myOptions = { zoom: 3, center: chicago, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); directionsDisplay.setMap(map); $('div#map_canvas').fadeIn(); }