Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mapa = {
- //dane inicjalizacyjne do googleMap
- // blokowanie funkcjonalnosci
- optionsMap: {
- streetViewControl: false,
- fullscreenControl: false,
- fullscreenControlOptions: false,
- mapTypeControl: false,
- panControl: false,
- minZoom: 11,
- maxZoom: 20,
- },
- //dane inicjalizacyjne do rysowania tras
- optionsBusLine: {
- strokeColor: 'red',
- strokeOpacity: 1.0,
- strokeWeight: 3,
- },
- isready: false,
- aktywnyPrzystanek: null,
- //zmienna przechowujaca trase autobusu po jego edycji
- busLineData: [],
- map: null,
- infowindow: null,
- //obraz przystanku/ inicjowany w init
- image: null,
- //dla aktywnego przystanku
- imageActive: null,
- markerActive: null,
- // Wsp. wszystkich przystankow
- allBusStop: [],
- // tablilica wsp do tworzenia markerow
- locations: [],
- // tablica zainicjowanych markerow
- markersArray: [],
- // trasa przejazdu
- busLine: null,
- // grupowanie Przystankow
- markerCluster: null,
- //dyrektywy rysowania lini tras
- directionsDisplay: [],
- init: function () {
- mapa.image = new google.maps.MarkerImage(
- document.location.origin + "/web/mpk_sf3/web/assets/img/busstop.png",
- new google.maps.Size(28, 50),
- new google.maps.Point(0, 0)
- );
- mapa.imageActive = new google.maps.MarkerImage(
- document.location.origin + "/web/mpk_sf3/web/assets/img/busstop-active.png",
- new google.maps.Size(28, 50),
- new google.maps.Point(0, 0)
- );
- mapa.initMap();
- },
- clearOverlays: function () {
- //czyszczenie mapy
- for (var i = 0; i < mapa.markersArray.length; i++) {
- mapa.markersArray[i].setMap(null);
- }
- mapa.markersArray.length = 0;
- google.maps.event.removeListener();
- if (mapa.markerCluster !== null) {
- mapa.markerCluster.clearMarkers();
- }
- if (mapa.busLine !== null) {
- mapa.busLine.setMap(null);
- }
- if (mapa.markerActive !== null) {
- mapa.markerActive.setMap(null);
- }
- if (mapa.infowindow !== null) {
- mapa.infowindow.close();
- }
- for (let i = 0; i < mapa.directionsDisplay.length; i++) {
- mapa.directionsDisplay[i].setMap(null);
- }
- },
- initMap: function () {
- mapa.clearOverlays();
- mapa.pokazWszystkiePrzystanki();
- },
- setMarkerActive: function (marker) {
- if (mapa.markerActive !== null) {
- mapa.markerActive.setIcon(mapa.image);
- }
- mapa.map.setCenter(marker.getPosition());
- mapa.map.setZoom(16);
- marker.setIcon(mapa.imageActive);
- mapa.markerActive = marker;
- },
- wyswietlWszystkiePrzystanki: function () {
- mapa.locations = mapa.allBusStop;
- mapa.wyswietlPrzystankiZLocation();
- },
- // Inicjowanie mapy z markerami wszystkich przystankow i listenerami po ich kliknieciu ( otwieraja rozklad na przystanku)
- // przystanki musza byc zaladowane do mapa.location
- // wyswietla przystanki i dodaje im listenerea po ktorego kliknieciu wyswietla rozklad na danym przystanku
- wyswietlPrzystankiZLocation: function (bezClusterow) {
- bezClusterow = bezClusterow || false;
- mapa.clearOverlays();
- var clusterStyles = [
- {
- url: document.location.origin + "/web/mpk_sf3/web/assets/img/busstop-group1.png",
- textColor: 'white',
- width: 22,
- height: 30
- },
- {
- url: document.location.origin + "/web/mpk_sf3/web/assets/img/busstop-group2.png",
- textColor: 'white',
- width: 28,
- height: 30
- },
- {
- url: document.location.origin + "/web/mpk_sf3/web/assets/img/busstop-group3.png",
- textColor: 'white',
- width: 34,
- height: 30
- }];
- var clusterOptions = {
- imagePath: document.location.origin + "/web/mpk_sf3/web/assets/img/busstop-group",
- maxZoom: 15,
- styles: clusterStyles
- };
- $.each(mapa.locations, function (k, val) {
- var latLng = new google.maps.LatLng(val[0], val[1]);
- var marker = new google.maps.Marker({
- position: latLng,
- anchorPoint: google.maps.Point(0, 20),
- icon: mapa.image,
- title: val[3],
- optimized: true,
- przystanek: k, // id przystanku, dodana wlasna wartosc
- map: mapa.map
- });
- mapa.markersArray.push(marker);
- google.maps.event.addListener(marker, 'click', function (mark, i) {
- mapa.setMarkerActive(marker);
- mapa.pokazPrzystanekZRozklademZAktualnaMapa(marker.przystanek);
- });
- });
- if (!bezClusterow) {
- try {
- mapa.markerCluster = new MarkerClusterer(mapa.map, mapa.markersArray, clusterOptions);
- } catch (err) {
- console.log("Błąd ładowania clusterów");
- }
- }
- },
- wysrodkujPinezkiZLocation: function () {
- var tempLenght = parseInt(Object.keys(mapa.locations).length / 2);
- tempLenght = Object.keys(mapa.locations)[tempLenght];
- var uluru = {lat: parseFloat(mapa.locations[tempLenght][0]), lng: parseFloat(mapa.locations[tempLenght][1])};
- mapa.map.setCenter(uluru);
- mapa.map.setZoom(14);
- },
- // uzupelnianie mapy o markery przystankow i trase przejazdu autobusu
- // listener ustawiony na wysylanie ajax'a z idRozkladKierunekTrasa
- wyswietlTrasePrzejazduZPrzystankami: function (tablicaPrzystankow, tablicaDrogi, aktualny) {
- aktualny = aktualny || null;
- mapa.locations = tablicaPrzystankow;
- var aktualnyMarkerWsp = null;
- if (aktualny !== null) {
- aktualnyMarkerWsp = new google.maps.LatLng(aktualny[0], aktualny[1]);
- }
- mapa.clearOverlays();
- mapa.busLineData = null;
- if (aktualny === null) {
- var tempLenght = parseInt(tablicaPrzystankow.length / 2);
- var uluru = {
- lat: parseFloat(tablicaPrzystankow[tempLenght][0]),
- lng: parseFloat(tablicaPrzystankow[tempLenght][1])
- };
- mapa.map.setCenter(uluru);
- } else {
- mapa.map.setCenter(aktualnyMarkerWsp);
- }
- mapa.map.setZoom(16);
- $.each(tablicaPrzystankow, function (k, val) {
- var latLng = new google.maps.LatLng(parseFloat(val[0]), parseFloat(val[1]));
- var marker = new google.maps.Marker({
- position: latLng,
- icon: aktualnyMarkerWsp == null ?
- mapa.image :
- latLng.equals(aktualnyMarkerWsp) ?
- mapa.imageActive :
- mapa.image,
- title: val[3].toString(),
- optimized: true,
- idRozkladu: val[2].toString(), // id rozkladu, dodana wlasna wartosc
- slug: val[4].toString(),
- map: mapa.map
- });
- if (latLng.equals(aktualnyMarkerWsp)) {
- mapa.markerActive = marker;
- }
- mapa.markersArray.push(marker);
- //sluchacz/ wyswietla rozklad po kliknieciu w marker
- google.maps.event.addListener(marker, 'click', function (mark, i) {
- var location = window.location.href.split('?')[1];
- location = location.split("&");
- var locationTemp = {};
- $.each(location, function (k, v) {
- var tempSplit = v.split("=");
- locationTemp[tempSplit[0]] = tempSplit[1];
- });
- location = locationTemp;
- location["przystanek"] = this.get('slug');
- mapa.zmienUrl(location);
- mapa.setMarkerActive(marker);
- mapa.pokazRozkladNaPrzystankuPoIdRozkladu(this.get('idRozkladu'));
- mapa.aktywujElementSidebar();
- var sidebar = document.getElementById("mainSidebarList").querySelectorAll('ul li');
- for (let i = 0; i < sidebar.length; i++) {
- if (sidebar[i].innerText == marker.title) {
- sidebar[i].classList.add("active");
- break;
- }
- }
- });
- });
- //rysowanie trasy start
- var points = mapa.getPoints(mapa.markerActive.idRozkladu);
- if (points == false) {
- var polyLines = [];
- $.each(tablicaDrogi, function (k, val) {
- var line = {lat: parseFloat(val[0]), lng: parseFloat(val[1])};
- polyLines.push(line);
- });
- } else {
- var polyLines = JSON.parse(points.punkty);
- }
- var partsPolyLines = mapa.createPartsLine(polyLines);
- for (let i = 0; i < partsPolyLines.length; i++) {
- mapa.drawingRoutesLine(mapa.map, partsPolyLines[i]);
- }
- },
- otherRoute: function(){
- mapa.clearOverlays();
- var otherRoute = $('#trasa').val();
- var points = mapa.getPoints(mapa.markerActive.idRozkladu, otherRoute);
- var polyLines = JSON.parse(points.punkty);
- var partsPolyLines = mapa.createPartsLine(polyLines);
- for (let i = 0; i < partsPolyLines.length; i++) {
- mapa.drawingRoutesLine(mapa.map, partsPolyLines[i]);
- }
- },
- // ustawianie pojedynczego przystanku na mapie i zwrocenie jego rozkladu
- wyswietlPrzstanekZRozkladem: function (markerWsp, rozkladHTML) {
- mapa.clearOverlays();
- mapa.wstawNowyRozklad(rozkladHTML);
- mapa.wyswietlWszystkiePrzystanki();
- var toActive = mapa.markersArray[markerWsp[2] - 1];
- mapa.setMarkerActive(toActive);
- },
- //Pobieranie trasy przejazdu autobusu
- pokazTrasnePrzejazduZPrzystankami: function (linia, kierunek) {
- if (linia !== "" && kierunek !== "") {
- $.ajax({
- method: "GET",
- url: $('#urlLiniaTrasa').val(),
- data: {
- 'linia': linia,
- 'kierunek': kierunek,
- 'mRozklad': $('#mRozklad :selected').val(),
- },
- success: function (json) {
- if (json.length == 1) {
- mapa.wyswietlTrasePrzejazduZPrzystankami(json[0], json[0]);
- } else {
- mapa.wyswietlTrasePrzejazduZPrzystankami(json[0], json[1]);
- }
- mapa.zmienUrl({'linia': linia, 'kierunek': kierunek});
- },
- error: function (data) {
- mapa.Status404Code(data);
- }
- });
- }
- },
- // pobieranie wszystkich przystankow/ w wypadku gdy wyszukiwarka jest pusta
- pokazWszystkiePrzystanki: function () {
- $.ajax({
- method: "get",
- url: $("#urlAxPobierzWszyskiePrzystanki").val(),
- data: {
- mRozklad: $('#mRozklad :selected').val()
- },
- success: function (data) {
- mapa.allBusStop = data;
- mapa.wyswietlWszystkiePrzystanki();
- mapa.isready = true;
- return true;
- },
- error: function (data) {
- mapa.Status404Code(data);
- }, complete: function () {
- wyszukiwarkaMap.init();
- }
- });
- },
- getPoints: function (id, otherRoute = null) {
- var points;
- var otherRoute = (otherRoute != null) ? otherRoute : null;
- console.log($("#AxRozkladKierunekTrasa").val() + '?id=' + id + '&o=' + otherRoute);
- $.ajax({
- method: "get",
- async: false,
- url: ($("#AxRozkladKierunekTrasa").val() + '?id=' + id + '&o=' + otherRoute),
- success: function (data) {
- points = data.result;
- },
- error: function () {
- alert('Błąd');
- }
- });
- return points;
- },
- // wyswietla rozklad na przystanku
- pokazPrzystanekZRozkladem: function (id) {
- $.ajax({
- method: "GET",
- url: $("#urlAxPobierzRozkladPrzystnku").val(),
- data: {
- przystanek: id,
- mRozklad: $('#mRozklad :selected').val(),
- },
- success: function (html) {
- mapa.zmienUrl(html[1]);
- if (html[3] != null) {
- mapa.wyswietlPrzstanekZRozkladem(html[2], html[0]);
- mapa.openInfoWindow(html[3]);
- } else {
- mapa.wstawNowyRozklad(html[0]);
- mapa.locations = html[2];
- mapa.wyswietlPrzystankiZLocation(true);
- }
- },
- error: function (data) {
- mapa.Status404Code(data);
- }
- });
- },
- pokazPrzystanekZRozklademZAktualnaMapa: function (id) {
- //po kliknieciu w marker przystanku
- $.ajax({
- method: "GET",
- url: $("#urlAxPobierzRozkladPrzystnku").val(),
- data: {
- przystanek: id,
- mRozklad: $('#mRozklad :selected').val()
- //nad chodzacy:$('#nad chodzacyRozklad').is(':selected'),
- },
- success: function (html) {
- mapa.wstawNowyRozklad(html[0]);
- mapa.zmienUrl(html[1]);
- mapa.openInfoWindow(html[3]);
- },
- error: function (data) {
- mapa.Status404Code(data);
- }
- });
- },
- pokazRozkladNaPrzystankuPoIdRozkladu: function (idRozkladu) {
- $.ajax({
- method: "GET",
- url: $("#urlPobierzRozkladPrzystankuDlaLiniPoIdRozkladu").val(),
- data: {
- idRozkladu: idRozkladu,
- mRozklad: $('#mRozklad :selected').val(),
- },
- success: function (data) {
- // mapa.zmienUrl(data[2]);
- if (mapa.markersArray.length <= 1) {
- mapa.wyswietlPrzstanekZRozkladem(data[1], data[0]);
- } else {
- mapa.wstawNowyRozklad(data[0]);
- }
- mapa.openInfoWindow(data[3]);
- },
- error: function (data) {
- mapa.Status404Code(data);
- }
- });
- },
- // przy wyszukani lini na przystanku wyswietla dla niej rozklad oraz marker na mapie
- pokazPrzystanekZRozklademDlaLini: function (przystanek, linia, kierunek) {
- kierunek = kierunek || null;
- $.ajax({
- method: "GET",
- url: $("#urlAxRozkladPrzystanekLiniaAction").val(),
- data: {
- przystanek: przystanek,
- linia: linia,
- kierunek: kierunek,
- mRozklad: $('#mRozklad :selected').val(),
- },
- success: function (data) {
- mapa.wstawNowyRozklad(data[0]);
- mapa.zmienUrl(data[1]);
- mapa.usunSidebar();
- // w przypadku kiedy uzytkownik nie wybierze opcji (Pokaz rozklad na przystanku) z autocomplete i wyszuka po fragmencie wpisanego slowa
- // wyswietlamy przystanki
- if (data[3] == null) {
- mapa.locations = data[2];
- mapa.wyswietlPrzystankiZLocation(true);
- } else {
- mapa.wstawSideBar(data[4]);
- var tab = data[2];
- if (tab.length == 1) {
- mapa.wyswietlTrasePrzejazduZPrzystankami(tab[0], tab[0], data[3]);
- } else {
- mapa.wyswietlTrasePrzejazduZPrzystankami(tab[0], tab[1], data[3]);
- }
- }
- },
- error: function (data) {
- mapa.Status404Code(data);
- }
- });
- },
- pokazTrasePrzejazduPoIdKieruneku: function (kierunek) {
- kierunek = kierunek || null;
- $.ajax({
- method: "GET",
- url: $("#urlAxRozkladPrzystanekLiniaAction").val(),
- data: {
- przystanek: przystanek,
- linia: linia,
- kierunek: kierunek,
- mRozklad: $('#mRozklad :selected').val(),
- },
- success: function (data) {
- mapa.wstawNowyRozklad(data[0]);
- mapa.zmienUrl(data[1]);
- mapa.usunSidebar();
- // w przypadku kiedy uzytkownik nie wybierze opcji (Pokaz rozklad na przystanku) z autocomplete i wyszuka po fragmencie wpisanego slowa
- // wyswietlamy przystanki
- if (data[3] == null) {
- mapa.locations = data[2];
- mapa.wyswietlPrzystankiZLocation(true);
- } else {
- mapa.wstawSideBar(data[4]);
- var tab = data[2];
- if (tab.length == 1) {
- mapa.wyswietlTrasePrzejazduZPrzystankami(tab[0], tab[0], data[3]);
- } else {
- mapa.wyswietlTrasePrzejazduZPrzystankami(tab[0], tab[1], data[3]);
- }
- }
- },
- error: function (data) {
- mapa.Status404Code(data);
- }
- });
- },
- // wyswietla kierunki lini w postaci listy/ mapa pozostaje pusta
- pokazLinieZTrasami: function (linia) {
- $.ajax({
- method: "GET",
- url: $("#urlAxRozkladLiniaAction").val(),
- data: {
- linia: linia,
- // mRozklad zmienione ze zmiennej nad chodzacy
- mRozklad: $('#mRozklad :selected').val()
- },
- success: function (html) {
- mapa.wstawNowyRozklad(html[0]);
- mapa.zmienUrl(html[1]);
- },
- error: function (data) {
- mapa.Status404Code(data);
- },
- complete: function () {
- mapa.usunSidebar();
- }
- });
- },
- // po wyszukaniu samej ulicy wyswietla liste przystankow na niej i dodaje pinzeki na mapie
- pokazPrzystankiNaUlicy: function (ulica) {
- $.ajax({
- method: "GET",
- url: $("#urlAxUlicaAction").val(),
- data: {
- ulica: ulica,
- mRozklad: $('#mRozklad :selected').val(),
- },
- success: function (html) {
- mapa.wstawNowyRozklad(html[0]);
- mapa.zmienUrl(html[1]);
- mapa.locations = html[2];
- mapa.wyswietlPrzystankiZLocation(true);
- mapa.wysrodkujPinezkiZLocation();
- },
- error: function (data) {
- mapa.Status404Code(data);
- },
- complete: function () {
- mapa.usunSidebar();
- }
- });
- },
- wstawNowyRozklad: function (html) {
- var elRozklad = document.getElementById("rozklady");
- $(elRozklad).empty();
- $(elRozklad).html(html);
- rozklad.pobierzPierwszyIntervalDoPelnejMinuty();
- },
- wstawSideBar: function (html) {
- var elSidebar = document.getElementById("kierunekSidebarDiv");
- $(elSidebar).empty();
- $(elSidebar).html(html);
- $(elSidebar).removeClass('hidden');
- $(elSidebar).addClass('col-sm-3 col-md-2');
- $('.mapa-box').removeClass('col-md-5');
- $('.mapa-box').removeClass('col-md-7');
- $('.mapa-box').addClass('col-md-5');
- },
- usunSidebar: function () {
- var elSidebar = document.getElementById("kierunekSidebarDiv");
- $(elSidebar).empty();
- $(elSidebar).removeClass('col-sm-3 col-md-2');
- $(elSidebar).addClass("hidden");
- $('.mapa-box').removeClass('col-md-5');
- $('.mapa-box').removeClass('col-md-7');
- $('.mapa-box').addClass('col-md-7');
- },
- aktywujElementSidebar: function (marker) {
- var lista = $('#mainSidebarList');
- var elementToActive = $(lista).find("li[class=\"active\"]");
- $.each(elementToActive, function (k, val) {
- $(val).removeClass("active");
- });
- elementToActive = $(lista).find("ul[class=\"active\"]");
- $.each(elementToActive, function (k, val) {
- $(val).removeClass("active");
- });
- if (marker != null) {
- marker = $(marker).parent();
- $(marker).addClass("active");
- $(marker).parent().addClass("active");
- }
- },
- zmienUrl: function (dane) {
- state = jQuery.param(dane);
- window.history.pushState(state, window.title, window.location.href.split('?')[0] + '?' + state);
- },
- Status404Code: function (data) {
- if (data.status == 404) {
- mapa.wstawNowyRozklad(data.responseJSON);
- }
- },
- openInfoWindow: function (content) {
- if (mapa.infowindow != null) {
- mapa.infowindow.close();
- }
- mapa.infowindow = new google.maps.InfoWindow({
- content: content,
- padding: 0,
- borderRadius: 0,
- });
- mapa.infowindow.open(mapa.map, mapa.markerActive);
- },
- //google ma limity w way pointach dlatego trase dzielimy na czesci
- createPartsLine: function (line) {
- const googleWayPointsLimit = 26;
- let partsLineNumber = Math.floor(line.length / googleWayPointsLimit);
- let partsLineNumberRest = line.length % googleWayPointsLimit;
- let i = 0;
- let lineParts = [];
- //pierwsza czesc normalnie od 1 do 26
- lineParts.push(line.slice(i * googleWayPointsLimit, i * googleWayPointsLimit + googleWayPointsLimit));
- i++;
- if (line.length > googleWayPointsLimit) {
- //kolejne czesci musza miec ostatni poprzedni punkt zeby sciezka byla polaczona
- for (null; i < partsLineNumber; i++) {
- lineParts.push(line.slice(i * googleWayPointsLimit - 1, i * googleWayPointsLimit + googleWayPointsLimit));
- }
- //tutaj dodajemy reszte z dzielenia waypointow
- if (partsLineNumberRest > 0) {
- lineParts.push(line.slice(i * googleWayPointsLimit - 1, i * googleWayPointsLimit + partsLineNumberRest));
- }
- }
- return lineParts;
- },
- //rysowanie wszystkich czesci
- drawingRoutesLine: function (map, partLine) {
- directionsService = new google.maps.DirectionsService;
- mapa.directionsDisplay[mapa.directionsDisplay.length] = new google.maps.DirectionsRenderer;
- (mapa.directionsDisplay[mapa.directionsDisplay.length - 1]).setMap(map);
- mapa.drawingPartLine(directionsService, mapa.directionsDisplay[mapa.directionsDisplay.length - 1], partLine);
- },
- //rysowanie trasy lini
- drawingPartLine: function (directionsService, directionsDisplay, line) {
- if (line.length > 0) {
- let waypoints = [];
- for (let i = 0; i < line.length; i++) {
- waypoints.push({
- location: line[i],
- stopover: true
- });
- }
- let start = waypoints[0].location;
- let end = waypoints[waypoints.length - 1].location;
- waypoints.shift();
- waypoints.pop();
- directionsService.route({
- origin: start,
- destination: end,
- travelMode: 'DRIVING',
- waypoints: waypoints,
- optimizeWaypoints: true,
- }, function (response, status) {
- if (status === 'OK') {
- directionsDisplay.setDirections(response);
- }
- });
- } else {
- console.log('Brak punktów.');
- }
- }
- };
- $(document).ready(function () {
- var uluru = {lat: 51.247867, lng: 22.558816};
- try {
- mapa.map = new google.maps.Map(document.getElementById('map'), {
- center: uluru,
- zoom: 12,
- options: mapa.optionsMap,
- });
- } catch (e) {
- window.location.reload();
- }
- mapa.init();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement