      //Begins 
     function loadScript() {
     	var script = document.createElement('script');
     	script.type = 'text/javascript';
     	//script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&key=AIzaSyAgYK3ytwWRRFM3OHhKQ88Uezpt3i-4NQI' +
		// '&callback=initialize';
     	script.src = 'https://maps.google.com/maps/api/js?libraries=geometry&sensor=true&key=AIzaSyCSsor1E1STB_jcqIGYc2ljfGBya5z5ULA' + '&callback=initialize';
     	document.body.appendChild(script);
     }

     window.onload = loadScript;
     //var executed=false;
     var projectpng='/static/images/google_maps_icon/google_maps_marker.png';
     var destpng='/static/images/google_maps_icon/red-map-marker.gif';
     var z;
     var map;
     
  	 var marker;
  	 //var destmarker;
  	 var mapOptions;
  	 var ZOOM=16;
     //Functions for current real time positions

     	function geocodePosition(pos) {
     		geocoder = new google.maps.Geocoder();
     		geocoder.geocode({
     			latLng: pos
     		},

     		function(results, status) {
     			if (status == google.maps.GeocoderStatus.OK) {
     				$('[name=location]').val(results[0].formatted_address);
     				$('[name=location_lat]').val(results[0].geometry.location.lat);
     				$('[name=location_lng]').val(results[0].geometry.location.lng);
                    $(".business_coords")[0].value = results[0].geometry.location.lat + "," + results[0].geometry.location.lng;
     				$("#mapErrorMsg").hide(100);
     			} else {
     				$("#mapErrorMsg").html('Cannot determine address at this location.' + status).show(100);
     			}
     		});
     	}   
     	function geocodePosition2(pos) {
     		geocoder = new google.maps.Geocoder();
     		geocoder.geocode({
     			latLng: pos
     		},

     		function(results, status) {
     			if (status == google.maps.GeocoderStatus.OK) {
     				$('[name=location2]').val(results[0].formatted_address);
     				$('[name=location_lat2]').val(results[0].geometry.location.lat);
     				$('[name=location_lng2]').val(results[0].geometry.location.lng);
                    $(".business_coords")[0].value = results[0].geometry.location.lat + "," + results[0].geometry.location.lng;
     				$("#mapErrorMsg").hide(100);
     			} else {
     				$("#mapErrorMsg").html('Cannot determine address at this location.' + status).show(100);
     			}
     		});
     	}   	
     	function geocodeAddress() {
     		var ajson = "https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyAgYK3ytwWRRFM3OHhKQ88Uezpt3i-4NQI&address=" + $('[name=location]').val()
     		$.ajax({
     			dataType: "json",
     			url: ajson,
     			success: function(d) {
     				z = d.results[0].geometry.location
     				$('[name=location_lat]').val(z.lat);
     				$('[name=location_lng]').val(z.lng);
     				marker.setPosition(z);
     				map.setCenter(z);
     			}
     		});
     	}
     	
     	function geocodeAddress2() {
     		var ajson = "https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyAgYK3ytwWRRFM3OHhKQ88Uezpt3i-4NQI&address=" + $('[name=location2]').val()
     		$.ajax({
     			dataType: "json",
     			url: ajson,
     			success: function(d) {
     				z = d.results[0].geometry.location
     				$('[name=location_lat2]').val(z.lat);
     				$('[name=location_lng2]').val(z.lng);
     				marker.setPosition(z);
     				map.setCenter(z);
     			}
     		});
     	}

     	function initializeProjectEdit() {
     		var posLatLngHome
     		posLatLngHome = new google.maps.LatLng('41.884821','-87.628118');
     		posLatLngDest = new google.maps.LatLng("<?= $project['location_lat2'] ?>", "<?= $project['location_lng2'] ?>");
     		//if($('[name=location_lat]').val() == '41.905464') //Default geolocation of nothing (in Chicago)
     		//{
     		//	posLatLngHome = new google.maps.LatLng("<?= $project['location_lat'] ?>", "<?= $project['location_lng'] ?>");
     		//} else {
     		//	posLatLngHome = new google.maps.LatLng($('[name=location_lat]').val(), $('[name=location_lng]').val());
     		//}
     		marker = new google.maps.Marker({
     			position: posLatLngHome,
     			draggable: true,
     			animation: google.maps.Animation.DROP,
     			map: map,
     			title: 'Pickup',
     			icon: projectpng

     		});
     		destmarker = new google.maps.Marker({
				 position: posLatLngDest,
				 draggable: true,
				 animation: google.maps.Animation.DROP,
				 map: map,
				 title: 'Destination',
				 icon: destpng

			 });
     		google.maps.event.addListener(marker, 'dragend', function() {
     			geocodePosition(marker.getPosition());
     			geocodePosition(destmarker.getPosition());
     		});
     	}
     	
     	function initialize() {
     	//var posLatLngHome = new google.maps.LatLng($('[name=location_lat]').val(), $('[name=location_lng]').val());
     	var posLatLngHome = new google.maps.LatLng('41.884821','-87.628118');
     	var posLatLngDest = new google.maps.LatLng('41.884821','-87.628118');
     	
     	var posPickup = new google.maps.LatLng($('[name=location_lat]').val(), $('[name=location_lng]').val());
     	var posDesto = new google.maps.LatLng($('[name=location_lat2]').val(), $('[name=location_lng2]').val());
 
      	//var posLatLngHome = new google.maps.LatLng(41.87905610633167, -87.63589344576346);
      	//var posLatLngDest
		//var posLatLngDest = new google.maps.LatLng(39.049122377688576, -94.4839049746849);
		//var posLatLngDest = new google.maps.LatLng($('[name=location_lat2]').val(), $('[name=location_lng2]').val());
     	
     	mapOptions = {
     		zoom: ZOOM,
     		center: posLatLngHome,
            scrollwheel: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP
     	}
     	// FROM OUR SCRIPT
		// var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
        map_elm = document.getElementById('map-canvas');
     	map = new google.maps.Map(map_elm, mapOptions);
     	
     	// Draw a line showing the straight distance between the markers
		//var line = new google.maps.Polyline({path: [posLatLngHome, posLatLngDest], map: map});
		var line = new google.maps.Polyline({path: [posPickup, posDesto], map: map});
     	
     	//https://jsfiddle.net/geocodezip/cgaxs4u8/
     	
     	marker = new google.maps.Marker({
     		position: posLatLngHome,
     		draggable: true,
     		animation: google.maps.Animation.DROP,
     		map: map,
     		title: 'Pickup',
     		icon: projectpng

     	});
     	destmarker = new google.maps.Marker({
			 position: posLatLngDest,
			 draggable: true,
			 animation: google.maps.Animation.DROP,
			 map: map,
			 title: 'Destination',
			 icon: destpng

		 });
		 
		 // Calculate and display the distance in miles
		 var distance = google.maps.geometry.spherical.computeDistanceBetween(posLatLngHome, posLatLngDest);
		 var distanceInMiles = distance * 0.000621371; // Conversion factor to miles

		 document.getElementById("distance").innerHTML = "Distance: " + distanceInMiles.toFixed(2) + " miles";
		 
     	google.maps.event.addListener(marker, 'dragend', function() {
      		geocodePosition(marker.getPosition());
      		geocodePosition(destmarker.getPosition());
      	});
     }