In leaflet, how to set a marker to a fixed position on map?

4k views Asked by At

For example, I want to set a leaflet marker always pin to the bottom-right corner of the leaflet map.

Does anyone have ideas about this?

2

There are 2 answers

0
ydoow On

You can make use of getCenter(), getSize(), and L.marker() to pin to the bottom-right.

Reference: http://leafletjs.com/reference.html

0
Dan On

There are a couple of ways to approach this, depending on what you want to accomplish.

  1. This request for a centered "floating" crosshairs icon sounds very similar to your request. You essentially use getCenter() to create a fixed reference point, then use setLatLng() to update your marker's location relative to the map's center each time the user pans.

  2. If the point you'd like to pin is purely cosmetic, you could just use HTML and CSS to position it on the page. This example, which is also referenced in the previous example, uses CSS to position an icon within a span as a visual reference point, then takes advantage of getCenter() to add lat/lon values to the form on the left of the page.