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

HW10-Zixuan Xu #12

Open
wants to merge 2 commits 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
36 changes: 32 additions & 4 deletions assignment/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ var resetApplication = function() {
map.removeLayer(state.line);

state.count = 0;
state.markers = []
state.markers = [];
state.line = undefined;
$('#button-reset').hide();
}
};

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

Expand All @@ -112,11 +112,39 @@ On draw

Leaflet Draw runs every time a marker is added to the map. When this happens
---------------- */
var getCoor = function() {
var coor = "";
_.each(state.markers,function(marker) {
coor += marker._latlng.lng+","+marker._latlng.lat+";";
});
return coor.substring(0,coor.length-1);
};

var getRoute = function () {
$.ajax("https://api.mapbox.com/optimized-trips/v1/mapbox/driving/"+getCoor()+
"?access_token=pk.eyJ1Ijoienh1YW54dSIsImEiOiJjanU0ODgxN3UwdmNtNDRvYm5mZmg0OWRpIn0.jDHEx7lWmzRBMqx2xHZ8-w").done(function(data){
var route = decode(data.trips[0].geometry);
state.line = L.polyline(route, {color: 'tomato'}).addTo(map);
});
};

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

var id = L.stamp(layer); // The unique Leaflet ID for the shape
var marker = L.marker(layer._latlng);
state.count+=1;
state.markers.push(marker);
map.addLayer(marker);
if (state.count==2) {
getRoute();
$('#button-reset').show();
}
else if(state.count>2) {
if (state.line != null) {
map.removeLayer(state.line);
}
getRoute();
}
console.log('Do something with the layer you just created:', layer, layer._latlng);
});
7 changes: 7 additions & 0 deletions lab/lab1/js/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,11 @@ 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 layer
if (myRectangle != null) {
map.removeLayer(myRectangle);
$('#shapes').empty();
}//task 3
myRectangle = layer;
map.addLayer(myRectangle);//task 2
$('#shapes').append( "<div class='shape' data-leaflet-id="+ id +"><h1>Current ID: "+ id +"</h1></div>" );//task 4
});