1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| var rendererOptions = {
draggable: true
};
(function(mapDemo, $, undefined) {
mapDemo.Directions = (function() {
function _Directions() {
var map,
directionsService, directionsDisplay,
autoSrc, autoDest, pinA, pinB,
markerA = new google.maps.MarkerImage('m1.png',
new google.maps.Size(24, 27),
new google.maps.Point(0, 0),
new google.maps.Point(12, 27)),
markerB = new google.maps.MarkerImage('m2.png',
new google.maps.Size(24, 28),
new google.maps.Point(0, 0),
new google.maps.Point(12, 28)),
// Caching the Selectors
$Selectors = {
mapCanvas: jQuery('#mapCanvas')[0],
dirPanel: jQuery('#directionsPanel'),
dirInputs: jQuery('.directionInputs'),
dirSrc: jQuery('#dirSource'),
dirDst: jQuery('#dirDestination'),
getDirBtn: jQuery('#getDirections'),
dirSteps: jQuery('#directionSteps'),
paneToggle: jQuery('#paneToggle'),
useGPSBtn: jQuery('#useGPS'),
paneResetBtn: jQuery('#paneReset')
},
autoCompleteSetup = function() {
autoSrc = new google.maps.places.Autocomplete($Selectors.dirSrc[0]);
autoDest = new google.maps.places.Autocomplete($Selectors.dirDst[0]);
}, // autoCompleteSetup Ends
directionsSetup = function() {
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
//directionsDisplay = new google.maps.DirectionsRenderer({
// suppressMarkers: true
//});
directionsDisplay.setPanel($Selectors.dirSteps[0]);
}, // direstionsSetup Ends
trafficSetup = function() {
// Creating a Custom Control and appending it to the map
var controlDiv = document.createElement('div'),
controlUI = document.createElement('div'),
trafficLayer = new google.maps.TrafficLayer();
jQuery(controlDiv).addClass('gmap-control-container').addClass('gmnoprint');
jQuery(controlUI).text('Traffic').addClass('gmap-control');
jQuery(controlDiv).append(controlUI);
// Traffic Btn Click Event
google.maps.event.addDomListener(controlUI, 'click', function() {
if (typeof trafficLayer.getMap() == 'undefined' || trafficLayer.getMap() === null) {
jQuery(controlUI).addClass('gmap-control-active');
trafficLayer.setMap(map);
} else {
trafficLayer.setMap(null);
jQuery(controlUI).removeClass('gmap-control-active');
}
});
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(controlDiv);
}, // trafficSetup Ends
mapSetup = function() {
map = new google.maps.Map($Selectors.mapCanvas, {
zoom: 11,
center: new google.maps.LatLng(34.20, -6.60),
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DEFAULT,
position: google.maps.ControlPosition.TOP_RIGHT
},
panControl: true,
panControlOptions: {
position: google.maps.ControlPosition.RIGHT_TOP
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.RIGHT_TOP
},
scaleControl: true,
streetViewControl: true,
overviewMapControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
autoCompleteSetup();
directionsSetup();
trafficSetup();
}, // mapSetup Ends
directionsRender = function(source, destination) {
$Selectors.dirSteps.find('.msg').hide();
directionsDisplay.setMap(map);
var request = {
origin: source,
destination: destination,
provideRouteAlternatives: false,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var _route = response.routes[0].legs[0];
pinA = new google.maps.Marker({position: _route.start_location, map: map, icon: markerA}),
pinB = new google.maps.Marker({position: _route.end_location, map: map, icon: markerB});
}
});
}, // directionsRender Ends
fetchAddress = function(p) {
var Position = new google.maps.LatLng(p.coords.latitude, p.coords.longitude),
Locater = new google.maps.Geocoder();
Locater.geocode({'latLng': Position}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var _r = results[0];
$Selectors.dirSrc.val(_r.formatted_address);
}
});
}, // fetchAddress Ends
invokeEvents = function() {
// Get Directions
$Selectors.getDirBtn.on('click', function(e) {
e.preventDefault();
var src = $Selectors.dirSrc.val(),
dst = $Selectors.dirDst.val();
directionsRender(src, dst);
});
// Reset Btn
$Selectors.paneResetBtn.on('click', function(e) {
$Selectors.dirSteps.html('');
$Selectors.dirSrc.val('');
$Selectors.dirDst.val('');
if(pinA) pinA.setMap(null);
if(pinB) pinB.setMap(null);
directionsDisplay.setMap(null);
});
// Toggle Btn
$Selectors.paneToggle.toggle(function(e) {
$Selectors.dirPanel.animate({'left': '-=305px'});
jQuery(this).html('>');
}, function() {
$Selectors.dirPanel.animate({'left': '+=305px'});
jQuery(this).html('<');
});
// Use My Location / Geo Location Btn
$Selectors.useGPSBtn.on('click', function(e) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
fetchAddress(position);
});
}
});
}, //invokeEvents Ends
_init = function() {
mapSetup();
invokeEvents();
}; // _init Ends
this.init = function() {
_init();
return this; // Refers to: mapDemo.Directions
}
return this.init(); // Refers to: mapDemo.Directions.init()
} // _Directions Ends
return new _Directions(); // Creating a new object of _Directions rather than a funtion
}()); // mapDemo.Directions Ends
})(window.mapDemo = window.mapDemo || {}, jQuery); |
Partager