diff --git a/webapp/class/json/example.json b/webapp/class/json/example.json index 62b0a40336a93891fe82c529ac813a24c828c1f4..3fe21171ed392bb44349c80b75469f3ec99f0346 100644 --- a/webapp/class/json/example.json +++ b/webapp/class/json/example.json @@ -17,6 +17,10 @@ "id": 0, "name": "Marc Vachon", "address": "Genève", + "coordinate":{ + "lon": 6.1466014, + "lat": 46.2017559 + }, "calendar": [{ "day": 27, "month": 5, @@ -68,6 +72,10 @@ "id": 1, "name": "Cholet Cholet", "address": "Berne", + "coordinate":{ + "lon": 9.8237266, + "lat": 46.7961744 + }, "calendar": [{ "day": 27, "month": 5, diff --git a/webapp/class/location.js b/webapp/class/location.js index 34c0442624e3ff6b0527cb5448ca4c2c64dfc158..99871165d2d1a235624a3b874c8274439a35f217 100644 --- a/webapp/class/location.js +++ b/webapp/class/location.js @@ -7,6 +7,8 @@ module.exports = { update_center: (idMeeting) => { db.get_locations(idMeeting, function(tableLoc){ + console.log(tableLoc); + find_center(tableLoc) }) } @@ -15,23 +17,15 @@ module.exports = { } function find_center(tableLoc){ - var x = 0; - var y = 0; - - var i = 1; - for (var val in tableLoc) { - console.log(val); - if(i % 2 != 0){ - x += val; - //console.log(x) - }else{ - y += val; - } - } - var size = tableLoc.length - 1 + var x = 0, y = 0; + + tableLoc.forEach(function(val){ + x += val.lon + y += val.lat + }) - x = x / size; - y = y / size; + x /= tableLoc.length + y /= tableLoc.length - return {lat: x, lon: y} + return {lat: y, lon: x} } diff --git a/webapp/ressources/db/db_request.js b/webapp/ressources/db/db_request.js index 20437ef5553c9f54aaf2801661075731078da44a..abd5421782b553fbd8ea108c94561d5ba2cdc69a 100644 --- a/webapp/ressources/db/db_request.js +++ b/webapp/ressources/db/db_request.js @@ -15,18 +15,29 @@ module.exports = { }) }, - get_locations: (id_meeting, callback) => { - let query = "SELECT data->'$.user_table' as user_table FROM t_meeting WHERE idMeeting = ?"; + get_locations: (idMeeting, callback) => { - db.query(query, id_meeting, (error, results, fields) => { - if (error) throw error; - - console.log(results[0]); + get_users(idMeeting, function(users){ + var tableLoc = new Array(users.length) - var json = JSON.parse(results) + for (var i = 0; i < users.length; i++) { + tableLoc[i] = {lat: users[i].coordinate.lat, lon: users[i].coordinate.lon} + } - callback(json) + callback(tableLoc) }) } } + +function get_users(id_meeting, callback){ + let query = "SELECT data->'$.user_table' as user_table FROM t_meeting WHERE idMeeting = ?"; + + db.query(query, id_meeting, (error, results, fields) => { + if (error) throw error; + + var json = JSON.parse(results[0]['user_table']) + + callback(json) + }) +} diff --git a/webapp/views/meeting.ejs b/webapp/views/meeting.ejs index 57f6e0ae1f0b0219787204419df3283f8980bd52..95f8dc2073fcc168a97a329b5efa2360254ada17 100644 --- a/webapp/views/meeting.ejs +++ b/webapp/views/meeting.ejs @@ -64,6 +64,9 @@ <h5 class="card-subtitle">Address</h5> <p><%= user['address'] %></p> + <h6 class="card-subtitle">Coordinate</h6> + <p>lat: <%= user['coordinate']['lat'] %>, lon: <%= user['coordinate']['lon'] %></p> + <h5 class="card-subtitle">Calendar</h5> <table class="table"> <% user['calendar'].forEach(function(date){ %>