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

Add/remove custom controls dynamically #128

Closed
paodb opened this issue May 16, 2024 · 4 comments
Closed

Add/remove custom controls dynamically #128

paodb opened this issue May 16, 2024 · 4 comments
Assignees

Comments

@paodb
Copy link
Member

paodb commented May 16, 2024

Feature proposal

Currently there seems not possible to add and remove custom controls dynamically (taken from #127 (comment))

@paodb paodb changed the title Add/remove custom controls dinamically Add/remove custom controls dynamically May 16, 2024
@github-project-automation github-project-automation bot moved this to Inbox (needs triage) in Flowing Code Addons May 16, 2024
@JariHanah
Copy link

Thanks for openning this issue,
Currently I have a use case where I need to navigate between data presented on the map. I have fixed buttons, I also have buttons that might be added by the user to filter the data. The user might ask to remove some filters all together,

I can possibly use the setCustomControls you recommended with removeAllCustomButtons you plan to add.
image

@JariHanah
Copy link

JariHanah commented Jun 1, 2024

To help with this,

I have written a code as a work around that solves the issue.
Even though the issue seems resolved with this code, after some updates and resetting the buttons, occasionally some of the newly inserted buttons gets inserted in a different position. not sure why, if it was a plugin or a google issue, or my code issue, .. or what is causing it. In any case, this new setCustomControls method, does help me add and remove buttons dynamically.

`
new Subclass to make getJson public
static class CustomControl2 extends CustomControl {
CustomControl2(Button b, ControlPosition pos) {
super(b, pos);
}

    public JsonObject getJson(int i) {
        return super.getJson(i);
    }
}

stores the elements to be removed if requested by user

List<Element[]> elements = new ArrayList<>();

this method can be called many times to reset the buttons with new buttons.

public void setControls(CustomControl2[] customControls) {
    JsonArray jsonArray = Json.createArray();
    elements.forEach(element -> {
        Arrays.stream(element).forEach(element1 -> {
            if (element1 != null) {
                gmaps.getElement().removeChild(element1);
            }
        });

    });
    elements.clear();
    for (int i = 0; i < customControls.length; ++i) {
        CustomControl2 customControl = customControls[i];
        jsonArray.set(i, customControl.getJson(i));
        customControl.getControlButton().getElement().setAttribute("slot", "customControlSlot_" + i);
        Element p = gmaps.getElement();
        Element[] e = new Element[]{customControl.getControlButton().getElement()};
        gmaps.getElement().appendChild(e);
        elements.add(e);
    }
    gmaps.getElement().setPropertyJson("customControls", jsonArray);
}

`

@JariHanah
Copy link

Another Workaround I did,
1 Is that I added all buttons into the MAP one time (so that I call setCustomControls one time,
2 then I control the visibility of the buttons with button.setVisible();

However, seems when you hide then show the buttons, the buttons are stacked over one another.
To solve this, run a setZoom(7), then a setZoom(1) this seems to force to reposition the buttons.(do this in separate threads)

@paodb paodb self-assigned this Jul 22, 2024
@paodb paodb moved this from Inbox (needs triage) to To Do in Flowing Code Addons Sep 16, 2024
paodb added a commit that referenced this issue Sep 23, 2024
@paodb paodb moved this from To Do to In Progress in Flowing Code Addons Sep 23, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in Flowing Code Addons Sep 26, 2024
@paodb
Copy link
Member Author

paodb commented Nov 4, 2024

Released with version 2.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants