bradcornford / Googlmapper

An easy way to integrate Google Maps with Laravel
MIT License
464 stars 142 forks source link

How can list current markers on the map ? #366

Closed t-prod closed 3 years ago

t-prod commented 4 years ago

Hi,

I try to display all markers on the map. To do it I use this :

Mapper::location('Paris')->map(['marker' => true, 'zoom' => 5, 'center' => true, 'eventBeforeLoad' => 'mapEvent(map);']);

Function in JS is like this but markers are undefined, do you have an idea ? :

<script type="text/javascript">
    function mapEvent(map) {
        let markers; // your markers
        let bounds = map.getBounds()
        for (let i=0; i<markers.length; i++){
            if(bounds.contains(markers[i].getPosition()) ){
                console.log(markers[i].content);
                // code for showing your object, associated with markers[i]
            }
        }
    }
</script>

Regards

bradcornford commented 4 years ago

You can only check the bounds of the map, once the map is actually loaded, so you need to use the event after load.


Mapper::location('Paris')->map(['marker' => true, 'zoom' => 5, 'center' => true, 'eventAfterLoad' => 'mapEvent(map, markers);']);
<script type="text/javascript">
    function mapEvent(map, markers) {
        let bounds = map.getBounds();

        for (let i=0; i<markers.length; i++){
            if(bounds.contains(markers[i].getPosition()) ){
                console.log(markers[i].getLabel());
            }
        }
    }
</script>