Problem with for each and Mapbox
So basically i'm having trouble with an click event firing a popup in Mapbox but the real issue is with
the for each function. I click the third icon in the sequence and it fires the previous two popups, if i click the second one in the sequence it will fire the second and first but not the third.
- echo '
- var map = new mapboxgl.Map({
- container: "map",
- style: "mapbox://styles/mapbox/streets-v9",
- center:[' . $longitude . ', ' . $latitude . '],
- zoom: 15
- });
- // add markers to map
- geojson.features.forEach(function(marker) {
- // create a DOM element for the marker
- var el = document.createElement("div");
- el.className = "marker";
-
-
-
- el.style.width = marker.properties.iconSize[0] + "px";
- el.style.height = marker.properties.iconSize[1] + "px";
-
-
-
- var popupsmall = new mapboxgl.Popup({
- closeOnClick: false
- })
- .setLngLat(marker.geometry.coordinates)
- //.setHTML(\'<img src="\' + marker.properties.placeImage + \'" style="width:150px; height:100px;padding:0px;"/>\')
- .setHTML("<h4 style=\"color:#6eebdf;margin-top:15px;font-size:24px;\">£" + marker.properties.ppw + "</h4>")
- .addTo(map);
- });
- geojson.features.forEach(function(marker) {
-
- $(".mapboxgl-popup").on("click", function(e) {
- var selected = $(e.target).attr("class");
-
- alert(this.id);
- var popup = new mapboxgl.Popup({
- closeOnClick: true
- })
- .setLngLat(marker.geometry.coordinates)
- .setHTML(\'<a href="./property.php?id=\' + marker.properties.propertyid + \'"><img src="\' + marker.properties.placeImage + \'" style="width:190px; height:100px;padding:0px;"/></a><br><p style="font-family: Hind, sans-serif; font-size:20px;margin-top:3px; padding-left:5px; ">\' + marker.properties.address2 + \'<br><h4 style=\"color:#11EFE0;margin-top:5px;font-size:20px; font-family: Hind, sans-serif; font-weight:600;\">£\' + marker.properties.ppw + \' Per week\')
- //.setHTML("<h4 style=\"color:#11EFE0;margin-top:5px;font-size:20px;\">£" + marker.properties.ppw + "</h4>")
- .addTo(map);
- console.log(marker.properties.address2);
- });
-
-
-
- map.on("click", function(e) {
- var popupsmall = new mapboxgl.Popup({
- closeOnClick: true
- })
- .setLngLat(marker.geometry.coordinates)
- //.setHTML(\'<img src="\' + marker.properties.placeImage + \'" style="width:150px; height:100px;padding:0px;"/>\')
- .setHTML("<h4 style=\"color:#6eebdf;margin-top:15px;font-size:24px;\">£" + marker.properties.ppw + "</h4>")
- .addTo(map);
- $(".mapboxgl-popup").on("click", function(e) {
- popupsmall.remove();
-
- var popup = new mapboxgl.Popup({
- closeOnClick: true
- })
- .setLngLat(marker.geometry.coordinates)
- .setHTML(\'<a href="./property.php?id=\' + marker.properties.propertyid + \'"><img src="\' + marker.properties.placeImage + \'" style="width:190px; height:100px;padding:0px;"/></a><br><p style="font-family: Hind, sans-serif; font-size:20px;margin-top:3px; padding-left:5px; ">\' + marker.properties.address2 + \'<br><h4 style=\"color:#11EFE0;margin-top:5px;font-size:20px; font-family: Hind, sans-serif; font-weight:600;\">£\' + marker.properties.ppw + \' Per week\')
- //.setHTML("<h4 style=\"color:#11EFE0;margin-top:5px;font-size:20px;\">£" + marker.properties.ppw + "</h4>")
- .addTo(map);
- });
- });
- });
-
-
-
- </script>';
Not too sure how the formatting looks here but it seems okay in my editor.
Greatly appreciated if any of you could help me.