Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change size of marker in leaflet

I have one marker on the map in leaflet:

var centerMarker = L.marker(centerPoint, { title: 'unselected' }).bindLabel(schools[i][0]);
centerMarker.on('click', selectMarker);
centerMarker.addTo(map);

I want to change the size of that marker on click.

I know that we can change icons but I just want to change the size of the same icon of the marker.

like image 886
vaibhav shah Avatar asked Apr 16 '13 13:04

vaibhav shah


People also ask

How do you add a custom marker in leaflet?

Marker Custom IconsStep 1 − Create a Map object by passing a <div> element (String or object) and map options (optional). Step 2 − Create a Layer object by passing the URL of the desired tile. Step 3 − Add the layer object to the map using the addLayer() method of the Map class.

How do you change the marker icon in leaflet react?

If you have the leaflet and react-leaflet npm modules installed, this should work for you. Click on the marker and you will see the popup with a "Change Marker Color" button. That should do the trick.

How many markers can leaflet handle?

The Clusterer can handle 10,000 or even 50,000 markers (in chrome).

What is marker in leaflet?

Use markers to call out points on the map. Marker locations are expressed in latitude/longitude coordinates, and can either appear as icons or as circles.


2 Answers

You can get the old icon from the marker itself, change the size of the icon and then call setIcon with the changed icon:

var icon = centerMarker.options.icon;
icon.options.iconSize = [newwidth, newheight];
centerMarker.setIcon(icon);
like image 62
m13r Avatar answered Sep 18 '22 10:09

m13r


Although it is an old question, but in case someone is looking for another possible option other than the answered ones.

L.marker([coord.latitude, coord.longitude], {
    color: 'red',
    icon: getIcon(), 
    data: coord
}).addTo(myMap).on("click", circleClick);

function getIcon(total_dead_and_missing) {
    var icon_size = [50, 50];   //for dynamic icon size, 
    var image_url = '1.png';        //for dynamic images

    L.icon({
        iconUrl: image_url,
        shadowUrl: 'leaf-shadow.png',

        iconSize:    icon_size , // size of the icon
        shadowSize:   [50, 64], // size of the shadow
        iconAnchor:   [22, 94], // point of the icon which will correspond to marker's location
        shadowAnchor: [4, 62],  // the same for the shadow
        popupAnchor:  [-3, -76] // point from which the popup should open relative to the iconAnchor
    });
}

Resource : https://leafletjs.com/examples/custom-icons/

like image 43
masud_moni Avatar answered Sep 19 '22 10:09

masud_moni