Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
- <script>
- var locations = []; //Array contains all locations of current date of all busses
- var buses = []; // array contains all buses details
- var iconlink = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|"; // this link generate random color giving color code at end
- var bouncingMarkers = []; // this array set the markers which should bounce (ie the last position of bus)
- var dateLastUpdate; // This value set date which map last updated
- var icons = []; // contains all icons color .
- var busRoutesArray = [];
- var busSelected = '';
- var previousStop = '';
- var nextStop = '';
- var busLatitude = '';
- var busLongitude = '';
- var markers = [];
- var polyLines = [];
- var map;
- var myLatLng;
- for(var i=0;i<100;i++){
- icons.push(iconlink+getRandomColor()); //creating random icon list
- }
- // this function generate random colors using above url
- function getRandomColor() {
- var letters = '0123456789ABCDEF';
- var color = '';
- for (var i = 0; i < 6; i++ ) {
- color += letters[Math.floor(Math.random() * 16)];
- }
- return color;
- }
- $(document).ready(function() {
- <%for (int i = 0; i < gpsData.size(); i++) {%>
- var gpsDataObject = {};
- gpsDataObject["busId"]= '<%=gpsData.get(i).getBusId()%>';
- gpsDataObject["lat"]= '<%=gpsData.get(i).getLatitude()%>';
- gpsDataObject["lng"]= '<%=gpsData.get(i).getLongitude()%>';
- gpsDataObject["date"]= '<%=gpsData.get(i).getDate()%>';
- locations.push(gpsDataObject);
- <%}%>
- <%for (int i = 0; i < busRoutes.size(); i++) {%>
- var busRoutesObject = {};
- busRoutesObject["busId"]= '<%=busRoutes.get(i).getBusId()%>';
- <%-- busRoutesObject["busName"]= '<%=busRoutes.get(i).getBus().getBusName()%>';
- busRoutesObject["schoolName"]= '<%=busRoutes.get(i).getBus().getSchoolName()%>'; --%>
- busRoutesObject["id"]= '<%=busRoutes.get(i).getId()%>';
- busRoutesObject["placeId"]= '<%=busRoutes.get(i).getPlaceId()%>';
- busRoutesObject["lat"]= '<%=busRoutes.get(i).getLatitude()%>';
- busRoutesObject["lng"]= '<%=busRoutes.get(i).getLongitude()%>';
- busRoutesObject["date"]= '<%=busRoutes.get(i).getDate()%>';
- busRoutesObject["bustop"]= '<%=busRoutes.get(i).getPlaceWeight()%>';
- busRoutesArray.push(busRoutesObject);
- <%}%>
- dateLastUpdate = <%=date%>;
- busRoutesArray.sort(function(a, b) {
- if(parseFloat(a.busId) == parseFloat(b.busId)){
- return parseFloat(a.bustop) - parseFloat(b.bustop);
- }else{
- return parseFloat(a.busId) - parseFloat(b.busId);
- }
- });
- <%for (int i = 0; i < busList.size(); i++) {%>
- var busesObject = {};
- busesObject["bus"]= '<%=busList.get(i).getBusName()%>';
- busesObject["school"]= '<%=busList.get(i).getSchoolName()%>';
- busesObject["id"]= '<%=busList.get(i).getId()%>';
- busesObject["previousStop"]= '<%=busList.get(i).getPreviousStop()%>';
- busesObject["nextStop"]= '<%=busList.get(i).getNextStop()%>';
- busesObject["latitude"]= '<%=busList.get(i).getLatitude()%>';
- busesObject["longitude"]= '<%=busList.get(i).getLongitude()%>';
- buses.push(busesObject);
- <%}%>
- for (var i = 0; i < buses.length; i++) {
- $("#info").append(
- '<div id="bodyContent">'
- + '<p><img src="'
- + icons[i % 100]
- + '"></img><b>Bus:</b>'
- + buses[i].bus + '</p>' +
- /* '<p><img src="'+icons[i%5]+'"></img><b>Bus:</b>'+buses[i].bus+', <b>School:</b>'+buses[i].school+'</p>'+
- */'</div>' + '</div>');
- }
- if(buses.length>0){
- busSelected = buses[0].id;
- previousStop = buses[0].previousStop;
- nextStop = buses[0].previousStop;
- busLatitude = buses[0].latitude;
- busLongitude = buses[0].longitude;
- if(previousStop == "" && buses.length>0){
- for(var j=0;j<busRoutesArray.length;j++){
- if(busRoutesArray[j].busId.trim() == buses[0].id.trim() ) {
- previousStop = busRoutesArray[j].id;
- nextStop = busRoutesArray[j].id;
- busLatitude = busRoutesArray[j].lat;
- busLongitude = busRoutesArray[j].lng;
- break;
- }
- }
- }
- $( "#busId" ).val(busSelected);
- }
- $( "#busId" ).change(function() {
- busSelected = $(this).val();
- for(var i=0;i<buses.length;i++){
- if(buses[i].id.trim() == busSelected.trim()){
- previousStop = buses[i].previousStop;
- nextStop = buses[i].previousStop;
- busLatitude = buses[i].latitude;
- busLongitude = buses[i].longitude;
- if(previousStop == "" && buses.length>0){
- for(var j=0;j<busRoutesArray.length;j++){
- if(busRoutesArray[j].busId.trim() == buses[i].id.trim() ) {
- previousStop = busRoutesArray[j].id;
- nextStop = busRoutesArray[j].id;
- busLatitude = busRoutesArray[j].lat;
- busLongitude = busRoutesArray[j].lng;
- break;
- }
- }
- }
- }
- }
- initMap();
- });
- });
- // Sets the map on all markers in the array.
- function setMapOnAll(map) {
- for (var i = 0; i < markers.length; i++) {
- markers[i].setMap(map);
- }
- for (var i = 0; i < polyLines.length; i++) {
- polyLines[i].setMap(map);
- }
- }
- //set icons based on each bus
- function getIcon(busId) {
- for (var i = 0; i < buses.length; i++) {
- if (buses[i].id.trim() == busId.trim()) {
- return icons[i % 100];
- }
- }
- }
- /* function getMarkerInfo(busId) {
- for (var i = 0; i < buses.length; i++) {
- if (buses[i].id.trim() == busId.trim()) {
- return '<div id="content">' + '<div id="siteNotice">'
- + '</div>'
- + '<h1 id="firstHeading" class="firstHeading">'
- + buses[i].bus + '</h1>' +
- '</div>';
- ;
- }
- }
- } */
- function getMarkerInfo(message) {
- return '<div id="content">' + '<div id="siteNotice">'
- + '</div>'
- + '<h4 id="firstHeading" class="firstHeading">'
- + message+ '</h4>' +
- /* '<div id="bodyContent">'+
- '<p><b>School:'+buses[i].school+'</b></p>'+
- '</div>'+ */
- '</div>';
- ;
- }
- //initiallizea and set all markers and info on map
- function reLoadMarekers() {
- setMapOnAll(null);
- var infowindow = new google.maps.InfoWindow();
- var marker, i;
- var bounds = new google.maps.LatLngBounds();
- var isPreviousLocationPassed = false;
- var isNextLocationPassed = false;
- var isFirst = true;
- for (i = 0; i < busRoutesArray.length; i++) {
- if (busRoutesArray[i].busId.trim() == busSelected.trim()) {
- marker = new google.maps.Marker({
- position : new google.maps.LatLng(parseFloat(busRoutesArray[i].lat),
- parseFloat(busRoutesArray[i].lng)),
- map : map,
- });
- markers.push(marker);
- if(isFirst){
- marker.setLabel("Start");
- isFirst = false;
- }
- markers.push(marker);
- bounds.extend(marker.getPosition());
- google.maps.event
- .addListener(
- marker,
- 'click',
- (function(marker, i) {
- return function() {
- infowindow
- .setContent(getMarkerInfo(busRoutesArray[i].placeId));
- infowindow.open(map, marker);
- }
- })(marker, i));
- var polyLineCoordinates = [];
- if (previousStop.trim() == busRoutesArray[i].id.trim()) {
- var markerImage = new google.maps.MarkerImage(
- "/img/greendot.png",
- null, /* size is determined at runtime */
- null, /* origin is 0,0 */
- new google.maps.Point(12, 12), /* anchor is bottom center of the scaled image */
- new google.maps.Size(25, 25)
- );
- marker.setIcon(markerImage);
- if((i < (busRoutesArray.length-1)) && (busRoutesArray[i].busId.trim() == busRoutesArray[i+1].busId.trim() )){
- var polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busLatitude);
- polyLineCoordinatesOBject['lng'] = parseFloat(busLongitude);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- var ployLine = new google.maps.Polyline({
- path : polyLineCoordinates,
- geodesic : true,
- strokeColor : '#11891A',
- strokeOpacity : 1.0,
- strokeWeight : 2,
- map: map
- });
- polyLines.push(ployLine);
- marker = new google.maps.Marker({
- position : new google.maps.LatLng(parseFloat(busLatitude),
- parseFloat(busLongitude)),
- map : map,
- });
- markers.push(marker);
- google.maps.event
- .addListener(
- marker,
- 'click',
- (function(marker, i) {
- return function() {
- infowindow
- .setContent(getMarkerInfo(busRoutesArray[i].placeId));
- infowindow.open(map, marker);
- }
- })(marker, i));
- var markerImage = new google.maps.MarkerImage(
- "/img/bluedot.png",
- null, /* size is determined at runtime */
- null, /* origin is 0,0 */
- new google.maps.Point(12, 12), /* anchor is bottom center of the scaled image */
- new google.maps.Size(25, 25)
- );
- marker.setIcon(markerImage);
- var polyLineCoordinates = [];
- var polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busLatitude);
- polyLineCoordinatesOBject['lng'] = parseFloat(busLongitude);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i+1].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i+1].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- var ployLine2 = new google.maps.Polyline({
- path : polyLineCoordinates,
- geodesic : true,
- strokeColor : '#C56E00',
- strokeOpacity : 1.0,
- strokeWeight : 2,
- map: map
- });
- }
- polyLines.push(ployLine2);
- isNextLocationPassed = true;
- } else if(isNextLocationPassed && isNextLocationPassed){
- var markerImage = new google.maps.MarkerImage(
- "/img/orangedot.png",
- null, /* size is determined at runtime */
- null, /* origin is 0,0 */
- new google.maps.Point(12, 12), /* anchor is bottom center of the scaled image */
- new google.maps.Size(25, 25)
- );
- marker.setIcon(markerImage);
- if((i < (busRoutesArray.length-1)) && (busRoutesArray[i].busId.trim() == busRoutesArray[i+1].busId.trim() )){
- var polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i+1].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i+1].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- var ployLine = new google.maps.Polyline({
- path : polyLineCoordinates,
- geodesic : true,
- strokeColor : '#C56E00',
- strokeOpacity : 1.0,
- strokeWeight : 2,
- map: map
- });
- polyLines.push(ployLine);
- }
- }else {
- var markerImage = new google.maps.MarkerImage(
- "/img/greendot.png",
- null, /* size is determined at runtime */
- null, /* origin is 0,0 */
- new google.maps.Point(12, 12), /* anchor is bottom center of the scaled image */
- new google.maps.Size(25, 25)
- );
- marker.setIcon(markerImage);
- if((i < (busRoutesArray.length-1)) && (busRoutesArray[i].busId.trim() == busRoutesArray[i+1].busId.trim() )){
- var polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i+1].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i+1].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- var ployLine = new google.maps.Polyline({
- path : polyLineCoordinates,
- geodesic : true,
- strokeColor : '#11891A',
- strokeOpacity : 1.0,
- strokeWeight : 2,
- map: map
- });
- polyLines.push(ployLine);
- }
- }
- }
- }
- if(marker != null){
- marker.setLabel("Stop");
- }
- }
- //initiallizea and set all markers and info on map
- function initMap() {
- myLatLng = {
- lat : parseFloat(9.9),
- lng : parseFloat(76)
- };
- if (busRoutesArray != null && busRoutesArray.length > 0) {
- var myLatLng = {
- lat : parseFloat(busRoutesArray[0].lat),
- lng : parseFloat(busRoutesArray[0].lng)
- };
- } else {
- alert("None of buses started from location");
- }
- map = new google.maps.Map(document.getElementById("map"), {
- zoom : 15,
- center : myLatLng
- });
- var infowindow = new google.maps.InfoWindow();
- var marker, i;
- var bounds = new google.maps.LatLngBounds();
- var isPreviousLocationPassed = false;
- var isNextLocationPassed = false;
- var isFirst = true;
- for (i = 0; i < busRoutesArray.length; i++) {
- if (busRoutesArray[i].busId.trim() == busSelected.trim()) {
- marker = new google.maps.Marker({
- position : new google.maps.LatLng(parseFloat(busRoutesArray[i].lat),
- parseFloat(busRoutesArray[i].lng)),
- map : map,
- });
- if(isFirst){
- marker.setLabel("Start");
- isFirst = false;
- }
- markers.push(marker);
- bounds.extend(marker.getPosition());
- google.maps.event
- .addListener(
- marker,
- 'click',
- (function(marker, i) {
- return function() {
- infowindow
- .setContent(getMarkerInfo(busRoutesArray[i].placeId));
- infowindow.open(map, marker);
- }
- })(marker, i));
- var polyLineCoordinates = [];
- if (previousStop.trim() == busRoutesArray[i].id.trim()) {
- var markerImage = new google.maps.MarkerImage(
- "/img/greendot.png",
- null, /* size is determined at runtime */
- null, /* origin is 0,0 */
- new google.maps.Point(12, 12), /* anchor is bottom center of the scaled image */
- new google.maps.Size(25, 25)
- );
- marker.setIcon(markerImage);
- if((i < (busRoutesArray.length-1)) && (busRoutesArray[i].busId.trim() == busRoutesArray[i+1].busId.trim() )){
- var polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busLatitude);
- polyLineCoordinatesOBject['lng'] = parseFloat(busLongitude);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- var ployLine = new google.maps.Polyline({
- path : polyLineCoordinates,
- geodesic : true,
- strokeColor : '#11891A',
- strokeOpacity : 1.0,
- strokeWeight : 2,
- map: map
- });
- polyLines.push(ployLine);
- marker = new google.maps.Marker({
- position : new google.maps.LatLng(parseFloat(busLatitude),
- parseFloat(busLongitude)),
- map : map,
- });
- markers.push(marker);
- google.maps.event
- .addListener(
- marker,
- 'click',
- (function(marker, i) {
- return function() {
- infowindow
- .setContent(getMarkerInfo(busRoutesArray[i].placeId));
- infowindow.open(map, marker);
- }
- })(marker, i));
- var markerImage = new google.maps.MarkerImage(
- "/img/bluedot.png",
- null, /* size is determined at runtime */
- null, /* origin is 0,0 */
- new google.maps.Point(12, 12), /* anchor is bottom center of the scaled image */
- new google.maps.Size(25, 25)
- );
- marker.setIcon(markerImage);
- var polyLineCoordinates = [];
- var polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busLatitude);
- polyLineCoordinatesOBject['lng'] = parseFloat(busLongitude);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i+1].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i+1].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- var ployLine2 = new google.maps.Polyline({
- path : polyLineCoordinates,
- geodesic : true,
- strokeColor : '#C56E00',
- strokeOpacity : 1.0,
- strokeWeight : 2,
- map: map
- });
- polyLines.push(ployLine2);
- }
- isNextLocationPassed = true;
- } else if(isNextLocationPassed && isNextLocationPassed){
- var markerImage = new google.maps.MarkerImage(
- "/img/orangedot.png",
- null, /* size is determined at runtime */
- null, /* origin is 0,0 */
- new google.maps.Point(12, 12), /* anchor is bottom center of the scaled image */
- new google.maps.Size(25, 25)
- );
- marker.setIcon(markerImage);
- if((i < (busRoutesArray.length-1)) && (busRoutesArray[i].busId.trim() == busRoutesArray[i+1].busId.trim() )){
- var polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i+1].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i+1].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- var ployLine = new google.maps.Polyline({
- path : polyLineCoordinates,
- geodesic : true,
- strokeColor : '#C56E00',
- strokeOpacity : 1.0,
- strokeWeight : 2,
- map: map
- });
- polyLines.push(ployLine);
- }
- }else {
- var markerImage = new google.maps.MarkerImage(
- "/img/greendot.png",
- null, /* size is determined at runtime */
- null, /* origin is 0,0 */
- new google.maps.Point(12, 12), /* anchor is bottom center of the scaled image */
- new google.maps.Size(25, 25)
- );
- marker.setIcon(markerImage);
- if((i < (busRoutesArray.length-1)) && (busRoutesArray[i].busId.trim() == busRoutesArray[i+1].busId.trim() )){
- var polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- polyLineCoordinatesOBject ={};
- polyLineCoordinatesOBject['lat'] = parseFloat(busRoutesArray[i+1].lat);
- polyLineCoordinatesOBject['lng'] = parseFloat(busRoutesArray[i+1].lng);
- polyLineCoordinates.push(polyLineCoordinatesOBject);
- var ployLine = new google.maps.Polyline({
- path : polyLineCoordinates,
- geodesic : true,
- strokeColor : '#11891A',
- strokeOpacity : 1.0,
- strokeWeight : 2,
- map: map
- });
- polyLines.push(ployLine);
- }
- }
- }
- }
- if(marker != null){
- marker.setLabel("Stop");
- }
- map.fitBounds(bounds);
- }
- // function used to recheck data base every minute to get current location of bus
- setInterval(function() {
- //used to access data base from remote thread
- var client = new XMLHttpRequest();
- client.onload = handler;
- client.open("GET", "getBusPosition.jsp?date=" + dateLastUpdate+"&busId="+busSelected);
- client.send();
- /* busLatitude = parseFloat(busLatitude) + 1;
- busLongitude = parseFloat(busLongitude) + 1;
- var data = [];
- var dataObject = {};
- dataObject['previousStop']=previousStop;
- dataObject['nextStop']=nextStop;
- dataObject['latitude']=busLatitude;
- dataObject['longitude']=busLongitude;
- data.push(dataObject);*/
- processData(data)
- }, 5000);
- //Some simple code to do something with data from an XML document fetched over the network:
- function processData(data) {
- if(data != null && data.length>0 && data[0].previousStop != "" && data[0].nextStop != "" && data[0].latitude != "" && data[0].longitude != ""){
- previousStop = data[0].previousStop;
- nextStop = data[0].nextStop;
- busLatitude = data[0].latitude;
- busLongitude = data[0].longitude;
- dateLastUpdate = new Date().getTime();
- reLoadMarekers();
- }
- }
- // XMLHttpRequest request return the responce to this function
- function handler() {
- if (this.status == 200 && this.response != null
- && this.response != '') {
- var newJson = this.response.replace(/'/g, '"');
- newJson = newJson.replace(/([^"]+)|("[^"]+")/g, function($0, $1, $2) {
- if ($1) {
- return $1.replace(/([a-zA-Z0-9]+?):/g, '"$1":');
- } else {
- return $2;
- }
- });
- var data = JSON.parse(newJson);
- // success!
- processData(data);
- } else {
- // something went wrong
- }
- }
- </script>
- <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC0ub2MijKbqO9wlDlGWFTb61r9m7zQ8dQ&callback=initMap">
- </script>
- </body>
- <!-- /. FOOTER -->
- <!-- SCRIPTS -AT THE BOTOM TO REDUCE THE LOAD TIME-->
- <!-- JQUERY SCRIPTS -->
- <script src="js/jquery-1.10.2.js"></script>
- <!-- BOOTSTRAP SCRIPTS -->
- <script src="js/bootstrap.js"></script>
- <!-- METISMENU SCRIPTS -->
- <script src="js/jquery.metisMenu.js"></script>
- <!-- CUSTOM SCRIPTS -->
- <script src="js/custom.js"></script>
- <%}else{
- response.sendRedirect("index.jsp");
- }%>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement