Search code examples
mapsgoogle-maps-markersmapboxmapbox-marker

addListener Google Maps in Mapbox


How can I put the type of addListener of google maps onto Mapbox instead of below one? Thanks for the response!

    stationI.addListener('click', function() {
        SetStationInfo(this.title);
    });

Herebelow is the full code:

for (var i=0;i<APIinfo.network.stations.length;i++){

    popup = new mapboxgl.Popup({ offset: 25 })
    .setText(APIinfo.network.stations[i].name); //Mapbox

    var marker = new mapboxgl.Marker()
        .setPopup(popup)
        .setLngLat([APIinfo.network.stations[i].longitude, APIinfo.network.stations[i].latitude])
        .addTo(map);    //Mapbox
                
                
    stationI.addListener('click', function() { //Google Maps
        SetStationInfo(this.title);
    });
}

Solution

  • Reading your code, not sure what stationI is, if it's a layer or an object.

    But regarding the events in mapbox, you can do that with:

    map.on('click', function(e) {
        console.log('A click event has occurred at ' + e.lngLat);
    });
    

    you can also add the layer:

    map.on('click', 'yourLayerId', function(e) {
        console.log('A click event has occurred at ' + e.lngLat);
    });