Interaction between two different Class

Interaction between two different Class
0
#1

Hello,

I created a class “Station” and a class “Map”.
In the station class there is data (via AJAX) that I recover via a loop “for of”.

It works. What I can not do is create a method in the Map class, which creates markers in relation to the data contained in the class Station (latitude and longitude)

Could someone explain to me how to do it?

#2

Hi Lora,
We don’t have enough context to help you sufficiently. Do you have code examples that we can look at?

#3

Hello !

Thank you for your answer !

Sure, the Station class :

export default class Station {
    constructor() {}

    getData(rep) {
        for( var info of rep ) {
            this.stationName = info.name;
            this.positionLat = info.position.lat;
            this.positionLng = info.position.lng;
        }
....

class Ajax :

... xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                var rep = JSON.parse(xhr.responseText);
                callback(rep);
                }
            }

index.js :

import Station from './src/Station.js';
let station = new Station();

Ajax.getData(url, (e) => { station.getData(e) } );

class MapLeaflet :

export default class MapLeaflet extends L.Class {
    constructor(conteneur, latitude, longitude) {
        super();
       .... }

    createMarker() {

            this.L.marker(
                [here position lat, position lng].addTo(this.myMap);
        }
    }
#4

It’s very odd to me to call that function getData, when it really sounds like a setter rather than a getter

Are stations expected to change their data or something? It seems like it should really be the constructor to me

Is there a reason you can’t use createMarker(station)?

1 Like
#5

Does need a bit more information about how/why this is structured the way it is, but I would ask

  • why does Station need to be a class?
  • why is there not a single request to get all the stations data at once?
  • how do you tell how many stations there are (what are you looping over!)?

Afaics, application should make a request for some station data. And it should initialise the map. Once it has both, add annotations by looping over the station data. All that 8s contained in one thing (class, file, module whatever), so it does those things in order and knows about both the map and the stations.

I assume this is Leaflet or similar, where you init a map, then you can add layers of annotations to it (in this case indicators for stations)?

1 Like
#6

Hello,

Thank you for the answer ! I am in training, I made this site without Station class :
www . veloca-lt . fr

It works but the instructions for this project is that EVERYTHING must be an object, and all classes must be in separate files.

So in fact, it works, but I do not respect the instructions.
I must have a Station class and a Map class :confused: