Skip to content

[Map] Console error when displaying a map using a Live Component #2417

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

Closed
LaurentSanson opened this issue Dec 2, 2024 · 4 comments · Fixed by #2420
Closed

[Map] Console error when displaying a map using a Live Component #2417

LaurentSanson opened this issue Dec 2, 2024 · 4 comments · Fixed by #2420
Assignees
Labels
Bug Bug Fix Map Status: Needs Review Needs to be reviewed

Comments

@LaurentSanson
Copy link

LaurentSanson commented Dec 2, 2024

After update to SF UX 2.2, i've got an error with showing the map using a Live Component.

Before 2.2, I had to create the ComponentWithMapTrait in my repo, and it worked just fine, but since the update today, I've got this error in the console when trying to see a Position on the map. On my form it still works well.

stimulus.index-b5b1d…b4a1e94e3bc92a.js:7 Error connecting controller

TypeError: Cannot read properties of null (reading 'lat')
    at Object.project (leaflet.index-0fc72e…45ac26ac.js:7:14529)
    at Object.latLngToPoint (leaflet.index-0fc72e…45ac26ac.js:7:13167)
    at e.project (leaflet.index-0fc72e…45ac26ac.js:7:37903)
    at e._getNewPixelOrigin (leaflet.index-0fc72e…45ac26ac.js:7:44363)
    at e._move (leaflet.index-0fc72e…45ac26ac.js:7:40715)
    at e._resetView (leaflet.index-0fc72e…45ac26ac.js:7:40423)
    at e.setView (leaflet.index-0fc72e…45ac26ac.js:7:28465)
    at e.initialize (leaflet.index-0fc72e…45ac26ac.js:7:27756)
    at new e (leaflet.index-0fc72e…e45ac26ac.js:7:2997)
    at Module.si (leaflet.index-0fc72e…45ac26ac.js:7:48247)
<?php

namespace App\Twig\Components;

use App\Entity\Position;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\Map\Live\ComponentWithMapTrait;
use Symfony\UX\Map\Map;
use Symfony\UX\Map\Marker;
use Symfony\UX\Map\Point;

#[AsLiveComponent]
final class MapComponent
{
    use DefaultActionTrait;
    use ComponentWithMapTrait;

    #[LiveProp]
    public ?Position $position = null;

    protected function instantiateMap(): Map
    {
        if (!$this->position?->getLatitude() && !$this->position?->getLongitude()) {
            return (new Map())
                ->center(new Point(48.8566, 2.3522))
                ->zoom(6);
        }

        return (new Map())
            ->addMarker(
                new Marker(
                    new Point(floatval($this->position->getLatitude()), floatval($this->position->getLongitude())),
                )
            )
            ->fitBoundsToMarkers();
    }
}

<twig:DashboardCard>
                    <twig:block name="title">Localisation</twig:block>
                    <twig:block name="content">
                        {% if mission.position.latitude %}
                            <div class="h-40 bg-gray-200 dark:bg-gray-700 rounded overflow-hidden">
                                <twig:MapComponent position="{{ mission.position }}"/>
                            </div>
                        {% else %}
                            <p class="text-gray-600 dark:text-gray-400">
                                Aucune position pour cette mission
                            </p>
                        {% endif %}
                    </twig:block>
                </twig:DashboardCard>
import {Controller} from "@hotwired/stimulus";

export default class extends Controller
{
    L;
    map;
    markers;
    static targets = ['latitude', 'longitude'];

    connect() {
        this._onConnect = this._onConnect.bind(this);
        this._onMapClick = this._onMapClick.bind(this);
        this.element.addEventListener('ux:map:connect', this._onConnect);
    }
    disconnect() {
        this.element.removeEventListener('ux:map:connect', this._onConnect);
    }

    _onConnect(event) {
        this.L = event.detail.L;
        this.map = event.detail.map;
        this.markers = event.detail.markers;

        this.map.on('click', this._onMapClick);
    }

    _onMapClick(event) {
        this.removeAllMarkers();
        this.createMarker(event.latlng)
        if (this.latitudeTarget && this.longitudeTarget) {
            this._addPositionToForm(event.latlng);
        }
    }

    createMarker(position) {
        const marker = this.L.marker(position).addTo(this.map);
        this.markers.push(marker);
    }

    removeAllMarkers() {
        this.markers.forEach(marker => marker.remove());
        this.markers = [];
    }

    _addPositionToForm(position) {
        this.latitudeTarget.value = position.lat;
        this.longitudeTarget.value = position.lng;
    }
}

@LaurentSanson LaurentSanson added the Bug Bug Fix label Dec 2, 2024
@carsonbot carsonbot added Status: Needs Review Needs to be reviewed Map labels Dec 2, 2024
@LaurentSanson LaurentSanson changed the title [Map] [Map] Console error when displaying a map Dec 2, 2024
@LaurentSanson LaurentSanson changed the title [Map] Console error when displaying a map [Map] Console error when displaying a map using a Live Component Dec 2, 2024
@Kocal Kocal self-assigned this Dec 2, 2024
@Kocal
Copy link
Member

Kocal commented Dec 2, 2024

Hi @LaurentSanson and sorry that.

Thanks to your reproducer, I've been able to reproduce it and I just opened #2420, can you tell me if it fixes your issue?

@LaurentSanson
Copy link
Author

LaurentSanson commented Dec 2, 2024 via email

@LaurentSanson
Copy link
Author

Morning @Kocal, I've tried it this morning and this is good for me! Well done 💪

@Kocal
Copy link
Member

Kocal commented Dec 3, 2024

Hey Laurent, perfect :)

I will merge soon, hoping for a new release fast (can be delayed a bit due to the SymfonyCon).
For the moment I suggest you to lock your symfony/ux-map dependencies to 2.21 🙏🏻

@Kocal Kocal closed this as completed in 56cef50 Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Map Status: Needs Review Needs to be reviewed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants