Skip to content

Commit a51b42d

Browse files
committed
feat: Refactor geolocation initialization and saving logic
- The attribute 'data-geolocation' has been changed to 'geolocation' - The variable 'geolocation_html' has been changed to 'element' - The method 'showPosition' has been renamed to 'showLocation' - The method 'saveData' has been renamed to 'save' - The method 'getPositionAsJSon' has been updated to return the position object directly - Removed unused code and console logs
1 parent 362b0dc commit a51b42d

File tree

1 file changed

+33
-77
lines changed

1 file changed

+33
-77
lines changed

src/location.js

Lines changed: 33 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,109 +4,65 @@ import { CoCreateMapAnimate } from './animate.js';
44
/*Class Custom*/
55

66
var CoCreateMapGetLocation = function () {
7-
//init custom
8-
/*query mongo
9-
> use mydb
10-
> db.getCollection("users").find({"_id": ObjectId("5de03b9cc745412976891133")})*/
11-
this.attr = 'data-geolocation';
12-
this.geolocation_html = null;
7+
this.attr = 'geolocation';
8+
this.element = null;
139

1410
this.init = () => {
1511
if (!this.ccMapAnimate) {
1612
this.ccMapAnimate = new CoCreateMapAnimate();
1713
}
18-
this.geolocation_html = object.querySelector("[" + this.attr + "]");
19-
if (this.geolocation_html && this.geolocation_html.getAttribute(this.attr) == 'true') {
20-
console.log("initialite map");
21-
this.showPosition();
14+
this.element = object.querySelector("[" + this.attr + "]");
15+
if (this.element && this.element.getAttribute(this.attr) == 'true') {
16+
console.log("initialize map");
17+
this.showLocation();
18+
19+
this.element.setValue = (data) => {
20+
this.createAnimator(data.object.currentLocation, data.object, data.object.icon);
21+
}
22+
23+
if (this.element.getValue)
24+
this.element.setValue(this.element.getValue())
25+
26+
this.element.getValue = () => {
27+
let coordinates = this.getPositionAsJSon(position)
28+
coordinates.icon = ''
29+
}
30+
31+
2232
}
23-
// CoCreateSocket.listen('update.object', (data) =>{
24-
// console.log(data);
25-
// if (data)
26-
// this.createAnimator(data.data.currentLocation, data.object, data.data.icon);
27-
// });
2833
}
2934

3035
this.createAnimator = (position, object, icon) => {
3136
this.ccMapAnimate.addAnimator(object, { 'lat': position.coords.latitude, 'lng': position.coords.longitude }, icon) // sending data
3237
}
3338

34-
this.saveData = (position) => {
35-
try {
36-
CoCreate.validateKeysJson(this.geolocation_html.dataset, ['array', 'object']);
37-
let array = this.geolocation_html.dataset['array'] || '';
38-
let object = this.geolocation_html.dataset['object'] || '';
39-
// textinput to object
40-
let icon = document.querySelector("input[data-animator='icon']").value;
41-
console.log(icon);
42-
if (icon) {
43-
icon = JSON.parse(icon);
44-
} else {
45-
icon = this.icon;
46-
}
47-
if (array != '')
48-
if (object != '') {
49-
console.log("Saved location in db", object);
50-
let obj = {
51-
method: 'update.object',
52-
array: array,
53-
object: {
54-
_id: object,
55-
currentLocation: this.getPositionAsJSon(position),
56-
icon: icon
57-
}
58-
};
59-
console.log(obj);
60-
CoCreate.crud.send(obj);
61-
} else {
62-
object = CoCreate.crud.send({
63-
method: 'create.object',
64-
array: array,
65-
object: {
66-
currentLocation: this.getPositionAsJSon(position),
67-
icon: icon
68-
}
69-
});
70-
console.log(object);
71-
this.geolocation_html.setAttribute("object", object);
72-
}
73-
}
74-
catch (e) {
75-
console.error(e);
76-
}
39+
this.save = () => {
40+
this.element.save()
7741
}
7842

7943
this.getPositionAsJSon = (position) => {
8044
return {
81-
coords: {
82-
accuracy: position.coords.accuracy,
83-
altitude: position.coords.altitude,
84-
altitudeAccuracy: position.coords.altitudeAccuracy,
85-
heading: position.coords.heading,
86-
latitude: position.coords.latitude,
87-
longitude: position.coords.longitude,
88-
speed: position.coords.speed,
89-
},
45+
accuracy: position.coords.accuracy,
46+
altitude: position.coords.altitude,
47+
altitudeAccuracy: position.coords.altitudeAccuracy,
48+
heading: position.coords.heading,
49+
latitude: position.coords.latitude,
50+
longitude: position.coords.longitude,
51+
speed: position.coords.speed,
9052
timestamp: position.timestamp,
91-
geoLocation: {
92-
latitude: position.coords.latitude,
93-
longitude: position.coords.longitude,
94-
}
9553
};
9654
}
9755

9856
this.showLocation = (position) => {
99-
// console.log(this);
100-
// Check position has been changed or not before doing anything
57+
// Check if the postion changed
10158
if (this.prevLat != position.coords.latitude || this.prevLong != position.coords.longitude) {
10259
// Set previous location
10360
this.prevLat = position.coords.latitude;
10461
this.prevLong = position.coords.longitude;
105-
// console.log(position);
106-
this.saveData(position);
107-
var positionInfo = "Your position is (" + "Latitude: " + position.coords.latitude + ", " + "Longitude: " + position.coords.longitude + ")";
108-
// document.getElementById("result").innerHTML = positionInfo;
10962

63+
this.element.save();
64+
let positionInfo = "Your position is (" + "Latitude: " + position.coords.latitude + ", " + "Longitude: " + position.coords.longitude + ")";
65+
console.log(positionInfo);
11066
}
11167
}
11268

0 commit comments

Comments
 (0)