6

I am trying to get the default marker NOT to show, but it's coming up WITH the icon that I want, so I have a marker and icon like this:

enter image description here

I'm getting my data from a OSM nomination search result.

if (response) {
        var mArray = [];
        for (var i = 0; i < response.length; i++) {
            var result = response[i];
            var s = result.display_name;
            var ex = result.extratags;
            var lk = ex.website;
            var ph = ex.phone;
            var hr = ex.opening_hours;
            var poly = result.geojson;
            if (hr) {
                var hours = hr.replace(/\;/g, '<br/>');
            }
            var arr = s.split(', ');
            var dd = s.substring(0, s.lastIndexOf(", "));
            var cc = dd.substring(dd.indexOf(",") + 1);
            var rIcon = result.icon;
            var myIcon;
            if (rIcon) {
                myIcon = new L.Icon({
                    iconSize: [35, 35],
                    iconAnchor: [13, 27],
                    popupAnchor: [1, -24],
                    iconUrl: rIcon
                });
            } else if (!(rIcon)) {
                myIcon = new L.Icon({
                    iconSize: [35, 35],
                    iconAnchor: [13, 27],
                    popupAnchor: [1, -24],
                    iconUrl: 'lib/images/question.png'
                });
            }
            var srchPops = "<div class=\"mypopupinfo\">";
            srchPops += "<div class='popHeader'><h6><strong>" + arr[0] + "</strong></div>";
            srchPops += "<div class='popBody'><h6>" + cc +
                    "</h6></div>";
            srchPops += "<button class='btn btn-default btn-xs add-field' value='" + result.lat + ", " + result.lon + "'>Add Point</button>";
            srchMarkers = new L.marker([result.lat, result.lon], {icon: myIcon}).bindPopup(srchPops);
            srchPoly = L.geoJson(poly);
            mArray.push(srchMarkers, srchPoly);
        }
    }
    srchresultMarkers = L.layerGroup(mArray);

So what am I doing wrong?

7

It looks strange that you add both a GeoJSON layer from result.geojson and a marker created from [result.lat, result.lon].

If result.geojson contains the same point information as [result.lat, result.lon], you are adding both markers to your map.

  • 1
    That's correct. I figured that out myself about 45 mins ago. :) – Dean Mar 16 '16 at 15:55
  • Good job in finding it out by yourself! :-) – ghybs Mar 16 '16 at 16:36
2

an event listener and change the icon through setIcon() method:

createdMarker.on("dblclick", function(evt) {

        var myIcon = L.icon({
            iconUrl: 'res/marker-icon-red.png',
            shadowUrl: 'res/marker-shadow.png'
        });
        this.setIcon(myIcon);
});

Or you can get some help from this Tutorial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.