Snippets

Piotr Szrajber Smart M.App - Capture click event

Created by Piotr Szrajber
/**
* TODO: surpress all other events from the layer
*/

function captureMapCoordinates(e, callback) {
    e.preventDefault();
    // changing icon to crosshair
    $("#map").css({
        cursor: "crosshair"
    });
    // using jQuery to capture click event
    $("#map").click(function(e) {
        $GP.utils.getInMapCrs({
            x: e.offsetX,
            y: e.offsetY
        }, callback);
        // unregister click event
        $("#map").off("click");
        $("#map").css({
            cursor: ""
        });
        // icon automatically changes to default one on map click event
    });
}

function doSomethingWithCoords(e) {
    captureMapCoordinates(e, function(coords) {
        let msg = `Coords: ${coords.x}, ${coords.y}`;
        gsp.ui.info(msg);
    });
}

gsp.ui.toolbar.add({
    id: "new-toolbar-item",
    title: "New toolbar item",
    style: "background-position: 0px 0px;" /* CHANGE_ICON */
}, function(ret) {
    ret.div.onclick = doSomethingWithCoords;
});

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.