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

Zixuan W_HW10 #4

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
24 changes: 22 additions & 2 deletions assignment/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,31 @@ On draw

Leaflet Draw runs every time a marker is added to the map. When this happens
---------------- */
var access = "pk.eyJ1IjoieHVhbjk2IiwiYSI6ImNqdTNnZ216YzBuM3M0ZHBlZjJ0ZTJ6MnoifQ.fzF7dByoN7k9L1pcyVSlwA"
var walkroute = function(){
$.ajax({
url: "https://api.mapbox.com/optimized-trips/v1/mapbox/walking/"+state.markers[0].coordinates+";"+state.markers[1].coordinates+"?access_token="+access,
success: function(result){
var latlngs = decode(result.trips[0].geometry);
state.line = L.polyline(latlngs, {color: 'red'}).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

console.log('Do something with the layer you just created:', layer, layer._latlng);
map.addLayer(layer);
state.count+=1;
state.markers.push(layer);
//set up semicolon-separated list coordinates for url
for (i=0; i<state.markers.length; i++){
state.markers[i].coordinates = state.markers[i]._latlng.lng+","+state.markers[i]._latlng.lat};
//
if (state.count==2){
walkroute();
$('#button-reset').show()}
else if (state.count>2){
if (state.line){ map.removeLayer(state.line);}}
});
34 changes: 33 additions & 1 deletion lab/lab1/js/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ Moving your mouse outside of the circle should remove the highlighting.

// Global Variables
var myRectangle;

// Initialize Leaflet Draw
var drawControl = new L.Control.Draw({
draw: {
Expand All @@ -85,9 +84,42 @@ var drawControl = new L.Control.Draw({

map.addControl(drawControl);

//Task1-4
/* =====================
// Event which is run every time Leaflet draw creates a new layer
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 (type === 'rectangle'){
if (myRectangle){
map.removeLayer(myRectangle);
$("#shapes").empty()
}
myRectangle = layer
map.addLayer(myRectangle)
$("#shapes").append('<div id="shapeid"></div>')
$("#shapeid").append('<h1>Current ID:' +id+ '</h1>' )
//OR do: //$("#shapes").append('<div class="shape" data-leaflet-id='+ id + '><h1>Current ID:'+ id +'</h1></div>')
}
});
===================== */

//Task5
var myRectangles =[];
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 (type === 'rectangle'){
if (myRectangles){
map.removeLayer(myRectangles);
$("#shapes").empty()
}
myRectangles.push(layer)
map.addLayer(layer)
$("#shapes").append('<div id="shapeid"></div>')
$("#shapeid").append('<h1>Current ID:' +id+ '</h1>' )
//or do: $("#shapes").append('<div class="shape" data-leaflet-id='+ id + '><h1>Current ID:'+ id +'</h1></div>')
}
});