Hi,
How to create a method for the reuse in two parts ?
I have a google map, and I have a code that displays the information of a marker when it clicks on it, the codes are almost the same, the variables only differ.
Example: In the first part, I have displayed 5 markers, click on them and the information is displayed:
marker.addListener('click', () => {
//create the content
let markerInfoTemplate = `
${this.locations[i][0]}
${this.locations[i][3]}
`;
this.infowindow.setContent(markerInfoTemplate);
this.infowindow.open(map, marker);
//change the markers icons
this.deselectAllMarkers();
marker.setIcon(this.activeIcon);
});
the 2nd Code, when looking for ex "catering" and several markers are displayed:
currentMarker.addListener('click', () => {
//create the content
let markerInfoTemplate = `
${place.name}
${place.adr_address}
`;
this.infowindow.setContent(markerInfoTemplate);
this.infowindow.open(map, currentMarker);
//change the markers icons
this.deselectAllMarkers();
currentMarker.setIcon(this.activeIcon);
});
This part would like to reuse it :
let markerInfoTemplate = `
${place.name}
${place.adr_address}
`;
As I said, the code is the same, except the variables. How can I restructure the code so it is not duplicated? Should I do a method? How could I do that? Thank you!
Vincent Maverick DuranoPosted Oct 16, 2018, 9:21 AM