admin管理员组

文章数量:1317915

I created and event listener to perform a function when the user drag a marker but it's not working, could anyone help me ?

This is the event listener

google.maps.event.addListener(marker, 'dragend', enviarParaASP(marker));

And the function:

function enviarParaASP(marcador) {
            coordenadaASalvar = { latitude: marcador.position.d, longitude: marcador.position.e };

            jQuery.ajax({
                url: 'GoogleMapsGeolocation.aspx/SalvaCoordenadas',
                type: "POST",
                data: JSON.stringify({'coord': coordenadaASalvar}),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) { alert("OK "); },
                failure: function (msg) { alert("Sorry!!! "); }
            });

I created and event listener to perform a function when the user drag a marker but it's not working, could anyone help me ?

This is the event listener

google.maps.event.addListener(marker, 'dragend', enviarParaASP(marker));

And the function:

function enviarParaASP(marcador) {
            coordenadaASalvar = { latitude: marcador.position.d, longitude: marcador.position.e };

            jQuery.ajax({
                url: 'GoogleMapsGeolocation.aspx/SalvaCoordenadas',
                type: "POST",
                data: JSON.stringify({'coord': coordenadaASalvar}),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) { alert("OK "); },
                failure: function (msg) { alert("Sorry!!! "); }
            });
Share Improve this question edited Jul 6, 2019 at 8:25 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 5, 2014 at 0:59 LuccasMFLuccasMF 4335 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Your syntax is incorrect:

google.maps.event.addListener(marker, 'dragend', enviarParaASP(marker));

will run your function and assign the return value as the listener function (not what you want, particularly since the function doesn't have a return value, so it will be null).

The line should be:

google.maps.event.addListener(marker, 'dragend', enviarParaASP);

And enviarParaASP should take a google.maps.MouseEvent as its argument.

Your callback should just be a reference to your function, not an invocation:

google.maps.event.addListener(marker, 'dragend', enviarParaASP);

By invoking the function, it's only going to "fire" when it appears in your code, which is when you're trying to set up the listener.

本文标签: javascriptEvent on marker drag Google Maps API V3Stack Overflow