Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature RM#75723: [TOUR] Display an itinerary #102

Open
wants to merge 1 commit into
base: wip
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 84 additions & 51 deletions src/main/webapp/map/directions.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<!-- <script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?sensor=false&region=FR">
</script> -->
<script src="../lib/jquery.ui/js/jquery.js" type="text/javascript"></script>
<script type="text/javascript" src="js/apphome.js"></script>
<script type="text/javascript">

Expand Down Expand Up @@ -72,59 +71,93 @@

function translateResult(element, status) {
var url = getAppHome() + "/ws/map/translation/" + "MapRest." + status;
$.get(url)
.done(function(data) {
element.innerHTML = data.translation;
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown );
element.innerHTML = status;
});
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(data) {
element.innerHTML = data.translation;
});
}

function initialize() {
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var dx = getQueryVariable("dx");
var dy = getQueryVariable("dy");
var ax = getQueryVariable("ax");
var ay = getQueryVariable("ay");

var rendererOptions = {
draggable: true
};

directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
var start = new google.maps.LatLng(dx, dy);
var end = new google.maps.LatLng(ax, ay);
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
translateResult(document.getElementById('directionsPanel') , status);
}
});
var mapOptions = {
zoom:7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: start
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directionsPanel'));

google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
computeTotalDistance(directionsDisplay.directions);
});

var url = getAppHome() + "/ws/map/" + getQueryVariable("object");
var id = getQueryVariable("id");
if (id) {
url += "/" + id;
}
fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(function (response) {
return response.json();
})
.then(function (result) {
if (result.status == 0) {
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var locations = [];
var errors = [];
for (var i = 0; i < result.data.length; i++) {
var { latit, longit ,address, errorMsg} = result.data[i];
if (errorMsg){
errors.push("\u2022 " +errorMsg);
continue;
}
locations.push(new google.maps.LatLng(latit, longit));
}
if (errors.length > 0) {
alert(errors.join("\n"));
}
var waypoints = locations.slice(1, -1).map(function (location) {
return {
location: location,
stopover: true
};
});

var rendererOptions = {
draggable: true
};

directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
var start = locations[0];
var end = locations[locations.length - 1];
var request = {
origin: start,
destination: end,
waypoints: waypoints,
optimizeWaypoints: false,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
translateResult(document.getElementById('directionsPanel'), status);
}
});
var mapOptions = {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: start
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directionsPanel'));

google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
computeTotalDistance(directionsDisplay.directions);
});
}
})
.catch(function (error) {
alert('Request failed: ' + error.message);
});
}

window.onload = function() {
Expand Down
96 changes: 69 additions & 27 deletions src/main/webapp/map/osm-directions.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<link rel="stylesheet"
href="https://unpkg.com/leaflet-routing-machine@latest/dist/leaflet-routing-machine.css" />

<style>
.leaflet-marker-icon{
width:0 !important;
height:0 !important;
}
</style>
</head>
<body>
<div id="map" style="width: 100%; height: 100%; position: absolute;"></div>
<script type="text/javascript" src="js/apphome.js"></script>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script
src="https://unpkg.com/leaflet-routing-machine@latest/dist/leaflet-routing-machine.js"></script>
Expand All @@ -31,35 +37,71 @@
</script>

<script>
var map = L.map('map').setView([44.907852, 7.673789],16);
var dx = getQueryVariable("dx");
var dy = getQueryVariable("dy");
var ax = getQueryVariable("ax");
var ay = getQueryVariable("ay");

var bounds = new L.LatLngBounds();
var marker1 = new L.marker(dx, dy);
var marker2 = new L.marker(ax, ay);
bounds.extend(marker1.getLatLng());
bounds.extend(marker2.getLatLng());
var map = L.map('map').setView([44.907852, 7.673789], 16);
var url = getAppHome() + "/ws/map/" + getQueryVariable("object");
var id = getQueryVariable("id");
if (id) {
url += "/" + id;
}
fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(function (response) {
return response.json();
})
.then(function (result) {
var waypoints = [];
var errors = [];
if (result.status == 0) {
for (var i = 0; i < result.data.length; i++) {
var { latit, longit, address, errorMsg} = result.data[i];
if (errorMsg){
errors.push("\u2022 " +errorMsg);
continue;
}
waypoints.push(L.Routing.waypoint(L.latLng(latit, longit),address));
}
if (errors.length > 0) {
alert(errors.join("\n"));
}
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>'
}).addTo(map);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>'
}).addTo(map);
L.Routing.control({
waypoints: waypoints,
routeWhileDragging: true,
addWaypoints: false,
geocoder: L.Control.Geocoder.nominatim(),
createMarker: function(i, waypoint, n) {
var popup = L.popup({
offset: [0, -30]
});
var marker = L.marker(waypoint.latLng ,{
draggable: true,
icon: L.divIcon({
html: '<div style="position: relative; display: inline-block; top:-35px; left:-6px;"><img src="../leaflet/images/marker-icon.png"><div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 12px; font-weight: bold; color: white;">' + String.fromCharCode(65 + i) + '</div></div>'
})
}).addTo(map)
.on('click', function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent(waypoint.name)
.openOn(map);
});
return marker;
}


L.Routing.control({
waypoints : [ L.latLng(dx, dy), L.latLng(ax, ay) ],
routeWhileDragging: true,
geocoder: L.Control.Geocoder.nominatim()
}).on('routingerror', function(e) {
alert('Route not Found !');
}).on('routingerror', function (e) {
alert('Route not Found !');
}).addTo(map);
}
})
.addTo(map);

map.fitBounds(bounds);

.catch(error => alert("Request failed: " + error));
</script>
</body>
</html>