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

HW#10_Mengting YU #15

Open
wants to merge 1 commit into
base: master
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
46 changes: 46 additions & 0 deletions assignment/css/main2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* =================
Add more styles here!
================= */

body {
padding: 0;
overflow: hidden;
font-family: helvetica, arial, sans-serif;
}

.school {
background-color: #ccc;
margin-bottom: 10px;
padding: 10px;
}
.truck {
margin: -10px -10px;
width: 20px;
height: 20px;
border: 2px solid #fff;
border-radius: 50%;
background: #3887be;
pointer-events: none;
}

#map {
position: absolute;
right: 0;
left: 0;
height: 100%;
top: 0;
}

#button-reset {
background-color: tomato;
padding: 10px;
position: absolute;
top: 10px;
left: 54px;
z-index: 2000;
border: 0;
border-radius: 3px;
color: #fff;
font-weight: 900;
display: none;
}
31 changes: 31 additions & 0 deletions assignment/index2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/0.4.9/leaflet.draw.css" />
<link href="https://api.tiles.mapbox.com/mapbox-gl-js/v0.53.1/mapbox-gl.css" rel="stylesheet">
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/main2.css" />
</head>
<body>

<div id='map' class='contain'></div>
<button id="button-reset">Reset Map</button>

<!-- End HTML -->

<!-- Start Javascript Imports -->

<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/0.4.9/leaflet.draw.js"></script>
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v0.53.1/mapbox-gl.js"></script>
<script src="https://npmcdn.com/@turf/turf/turf.min.js"></script>
<script src="js/decode.js"></script>
<script src="js/main2.js"></script>

<!-- End Javascript Imports -->

</body>
</html>
45 changes: 42 additions & 3 deletions assignment/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var state = {
line: undefined,
}


/** ---------------
Map configuration
---------------- */
Expand Down Expand Up @@ -98,15 +99,39 @@ function. That being said, you are welcome to make changes if it helps.
var resetApplication = function() {
_.each(state.markers, function(marker) { map.removeLayer(marker) })
map.removeLayer(state.line);

if(state.line != undefined) {
map.removeLayer(state.line);
}
state.count = 0;
state.markers = []
state.line = undefined;
$('#button-reset').hide();
$('.leaflet-draw-draw-marker').show();
}

$('#button-reset').click(resetApplication);

var getCoord = () => {

lng1 = state.markers[0].getLatLng()['lng'];
lat1 = state.markers[0].getLatLng()['lat'];
lng2 = state.markers[1].getLatLng()['lng'];
lat2 = state.markers[1].getLatLng()['lat'];
coords = lng1 + ", " + lat1 + " to " + lng2 + ", " + lat2;
console.log(coords);
return coords;
}

var routing = () => {
mapboxtoken = "pk.eyJ1IjoiYXN0b3JpYSIsImEiOiJjanR1aWV5aTgwdG5qNDVtcDdkcGh4bWU3In0.6LPQecMoTd7G0V0APrkg3w";
$.ajax({
url: "https://api.mapbox.com/directions/v5/mapbox/driving/" + getCoord() + ".json?access_token=" + mapboxtoken,
success: function(routes) {
var geometry = decode(routes['routes'][0]['geometry']);
state.line = L.polyline(geometry, {color: 'orange'}).addTo(map);
}
})
}
/** ---------------
On draw

Expand All @@ -117,6 +142,20 @@ map.on('draw:created', function (e) {
var type = e.layerType; // The type of shape
var layer = e.layer; // The Leaflet layer for the shape
var id = L.stamp(layer); // The unique Leaflet ID for the

console.log('Do something with the layer you just created:', layer, layer._latlng);
console.log('Start drawing:', layer, layer._latlng);

// Draw markers
var marker = L.marker(layer._latlng);
state.count ++;
state.markers.push(marker);
console.log(state.count);
map.addLayer(marker);

if(state.count > 0) {
$('#button-reset').show(); // show the reset button when there is markers
}
if(state.count == 2 ){
routing();
$('.leaflet-draw-draw-marker').hide();
}
});
Loading