2016-06-10 70 views
0

我目前在使用PNG作为标记,但我想将点击&活动状态的标记更改为不同的png。单击更改Mapbox标记图像

我该如何做到这一点?

 var icon = [ 
      { 
       "className": "my-icon icon-dc", // class name to style 
       "html": '<img class="style-svg" src="wp-content/uploads/2016/06/circle.svg" alt="">', // add content inside the marker 
       "iconSize": [60,60] // size of icon, use null to set the size in CSS 
      } 
     ]; 

    myLayer.on('layeradd', function(e) { 
     var marker = e.layer, 
      feature = e.layer.feature; 

     marker.setIcon(L.divIcon(feature.properties.icon)); 
    }); 


     // Listen for individual marker clicks. 
    myLayer.on('click',function(e) { 
     // Force the popup closed. 
     e.layer.closePopup(); 

     var feature = e.layer.feature; 
     var content = '<div><div id="close1" class="absolute_p"><img class="exit" src="/wp-content/uploads/2016/06/[email protected]"></div>' + 
         '<img src="' + feature.properties.img + '" alt="">' + 
         '<h4>' + feature.properties.title + '</h4>' + 
         '<p>' + feature.properties.description + '</p>' + 
         '<p class="num">' + feature.properties.num + 'Properties now showing</p>' + 
         '<a href="'+ feature.properties.url +'" class="btn btn-default btn-lg absolute_p" target="_blank">find your second home here</a></div>'; 

     info.innerHTML = content; 
     map.panTo(e.layer.getLatLng()); 

    }); 

回答

0

在您click事件处理结束,为什么不直接使用在e.layer与参考setIcon到“主动” /“点击”图标声明别的地方?

var myNewIcon = L.icon({ 
    iconUrl: 'my-new-icon.png' 
}); 

e.layer.setIcon(myNewIcon) 

http://leafletjs.com/reference.html#icon

+0

这工作但是,如果我点击其他标记标记不改回旧 – PatrickC

+0

我想通了。结合jquery将所有标记的src更改为旧标记作品! – PatrickC

+0

或者你可以用'.eachLayer()'循环标记并自己重置它们。 – snkashis