This project has moved and is read-only. For the latest updates, please go here.

count mouse click on AjaxMapControl

Jan 20, 2011 at 4:21 PM

Hi everybody,

Can anybody tell me how to count mouse click on AjaxMapControl.

I am trying to count distance between two point. My idea: When user first press -> get X, Y into 2 textboxes, when user second press -> get X, Y into other 2 textboxes. Because I think when we have coordinates of two points we can count the distance between them. Can I handle in this way ? Or you know any way better, please tell me.


Jan 20, 2011 at 7:28 PM

Hi namserious

You can measure the distance between two points (p1, p2) like this

    var d1 = SharpMap_PixelToMap(p1.x, p1.y, obj);
    var d2 = SharpMap_PixelToMap(p2.x, p2.y, obj);
    var dist = (Math.sqrt(Math.pow(d2.x - d1.x, 2) + Math.pow(d2.y - d1.y, 2)));
This code is extracted from MNorm's ajaxMap extension, maybe you can adapt his measure tool to your program.
Find the source code here (
Jan 21, 2011 at 2:48 AM

Hi carjona

Do you mean before I get coordinates I had to "freeze" AjaxMapControl ?

Sharpmap has control "query map" when I select it, ajaxMapControl will "freeze" and I can get X, Y. What I want is how to get X, Y between 2 clicked times.

e.g: Via your code, how can I get p1, p2. Can I use this code ?

var mousePos = SharpMap_GetRelativePosition(event.clientX,event.clientY,obj.container);
	var pnt = SharpMap_PixelToMap(mousePos.x,mousePos.y,obj);

Jan 21, 2011 at 1:29 PM

You can try to get X, Y between 2 clicked times using some HiddenFields, but the code I posted is for client side javascript AjaxMap.js

If you get into this code you will find that during a drag operation (MouseDown --> MouseOver --> MouseUp), you can get initial point (startDrag variable) and end point (endDrag variable).

The MNorm's ajaxMap control has it's own "Measure Tool" that when it is selected, instead of dragging the map draws a line and measure the distance of the line. This way its more elegant than clicking the map 2 twice.

If you adapt this code to your program you will have to download the link posted and look at ajaxMapControl.cs and AjaxMap.js 


Feb 8, 2011 at 2:04 AM
Edited Feb 8, 2011 at 2:14 AM

I'm back, I had to postpone my work for a long time. Thank you, carjona for giving the code, my leader also requires me reading this code (after I asked you hah).