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

[Map][Leaflet] Provide example to load via AJAX #2361

Open
tacman opened this issue Nov 10, 2024 · 4 comments
Open

[Map][Leaflet] Provide example to load via AJAX #2361

tacman opened this issue Nov 10, 2024 · 4 comments
Labels
docs Improvements or additions to documentation

Comments

@tacman
Copy link
Contributor

tacman commented Nov 10, 2024

I'd like to load my map point from JSON (specifically, api-platform), can you provide an example of how to do that in the custom controller please? I'm sure it's easy once you get the Leaflet object, but I'm not sure where to get it from.

If there are other events to listen for, like the map moving or being zoomed, can you provide a stub with a console.log to the event data?

@tacman tacman added the docs Improvements or additions to documentation label Nov 10, 2024
@tacman
Copy link
Contributor Author

tacman commented Nov 10, 2024

I see how to do it now, though it can probably be improved.

async _onConnect(event) {
    // The map, markers and infoWindows are created
    // The instances depend on the renderer you are using
    const map = event.detail.map;
    const url = "/api/members.json";
      const response = await fetch(url);
      if (!response.ok) {
        throw new Error(`Response status: ${response.status}`);
      }

    try {
      await response.json().then(
        data => {
          data.data.forEach(obj => {
            console.log(obj);
            L.marker([obj.mailingLatitude, obj.mailingLongitude], {
              title: obj.displayName
            }).addTo(map);
          })
        }
      );
    } catch (error) {
      console.error(error.message);
    }

    // console.log(event.detail.map);
    // console.log(event.detail.markers);
    // console.log(event.detail.infoWindows);
  }

@smnandre smnandre changed the title [LeafletComponent] Provide example to load via AJAX [Map][Leaflet] Provide example to load via AJAX Nov 10, 2024
@Kocal
Copy link
Member

Kocal commented Nov 14, 2024

Hi @tacman and sorry for the late reply.

The UX Map was not designed to be able to dynamically load/create markers/info-windows/... after a XHR request, but rather using a Live Component (which I think it possible yet, BUT will suffer of webperf issues).

Load/create markers dynamically after a XHR request is not an easy task like as you have seen, you won't have access to internal createMarker from Map Stimulus controllers. Instead you have to listen to ux:map:pre-connnect or ux:map:connect events to get google (or L when using Leaflet) from event.detail, in order to use the provider's API.

@tacman
Copy link
Contributor Author

tacman commented Nov 14, 2024

ok, thanks! The code I wrote in the comment above works, though the marker is the default and needs to be cleaned up.

I ran into some conflict with Turbo/Live Components when they were still in beta, but need to return to using them. Is there an example of this map using Live Components?

@Kocal
Copy link
Member

Kocal commented Nov 15, 2024

Is there an example of this map using Live Components?

Not yet, it is not fully compatible

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

No branches or pull requests

2 participants