How to find the point coordinates for marker?

6,283 views
Skip to first unread message

Mike George

unread,
Mar 19, 2012, 12:13:27 PM3/19/12
to Leaflet
Hello, could somebody give me an idea how to find the x,y coordinates
of a marker when it's clicked on please?

I can find the location with code like this:

marker.on('click',function(e){alert(markerLocation);});

but not the point coordinates.

I have tried to use the latLongToLayerPoint conversion method, but I'm
not very familiar with oop and I'm not sure how to use it.

Any help would be very much appreciated.

Thanks.

Luiz Felipe Pedone

unread,
Mar 19, 2012, 12:51:50 PM3/19/12
to leafl...@googlegroups.com
You can just  call the method getLatLng()of your marker object (marker.getLatLng()).

Atenciosamente,
Luiz Felipe Pedone
http://curriculo.luizpedone.com/

Mike George

unread,
Mar 19, 2012, 1:05:35 PM3/19/12
to Leaflet
Thanks, but I don't want the latitude and longitude, I want the point
coordinates in pixels from the top and left of the container div.


On Mar 19, 4:51 pm, Luiz Felipe Pedone <luiz.ped...@gmail.com> wrote:
> You can just  call the method getLatLng()of your marker object
> (marker.getLatLng()).
>
> Atenciosamente,
> Luiz Felipe Pedonehttp://curriculo.luizpedone.com/<http://www.luizpedone.com/>

Fabien Nicollet

unread,
Mar 19, 2012, 6:32:13 PM3/19/12
to leafl...@googlegroups.com
Hi,
I believe u should use the method latLngToContainerPoint on L.Map:
Works for me, not for marker but for other stuff

Fabien

Mike George

unread,
Mar 20, 2012, 2:00:00 AM3/20/12
to Leaflet
Thanks Fabien, I'm sure that's the one I want, I think it's just that
I don't understand how to use it.

I'm using v0.3.1 which doesn't seem to have that method, so I'm
experimenting with latLngToLayerPoint as follows:

var map1 = new L.Map('dashMap1',mapOptions);
var aLocation = new L.LatLng(53, -2);
var aPoint = new L.Point(map1.latLngToLayerPoint(aLocation));
alert (aPoint);

which gives the error message:

Error: Unable to get value of the property 'x': object is null or
undefined

What am I doing wrong?

Fabien Nicollet

unread,
Mar 20, 2012, 5:06:15 AM3/20/12
to leafl...@googlegroups.com
Hi,
You don't have to pass it into a L.Point constructor, it already returns a L.Point. So the correct code is:
var aPoint = map1.latLngToLayerPoint(aLocation);
       alert (aPoint); 

Fabien
--
Fabien Nicollet
Tel : +33 (0)6 60 54 72 20

Mike George

unread,
Mar 20, 2012, 6:01:01 AM3/20/12
to Leaflet
Yes, that seems to be it and the test is working. I was also trying to
find the point before I'd set the view, which didn't work!


On Mar 20, 9:06 am, Fabien Nicollet <fnicol...@gmail.com> wrote:
> Hi,
> You don't have to pass it into a L.Point constructor, it already returns a
> L.Point. So the correct code is:
> var aPoint = map1.latLngToLayerPoint(aLocation);
>        alert (aPoint);
>
> Fabien
>

Suresh Kumar

unread,
Nov 3, 2015, 7:33:03 AM11/3/15
to Leaflet
I find a solution

let me make a doc then i show you.. :-)


Reply all
Reply to author
Forward
0 new messages