tags:

views:

255

answers:

2

Hi There, I have the following code for my google maps app. What I want to do is be able to click or drag the markers and get the new position (LatLong) of the point(marker).

At the moment when I do anything of the sort i get the coords 37.4419, -122.1419 . I am sure this is because it is speicied as the center. How do I get the coords of the event/marker that was just triggered/draged,clicked?

Please excuse me as I am very inexpereience with both Javascript and the Google maps API.

Code:

        var map = null;
    var geocoder = null;
    var zoom = 15;
    var first_point = false;
    var boundary =  new Array();
    var cCount = 0;

    var point1;
    var point2;

    function initialize() {
        if (GBrowserIsCompatible()) {
            first_point = false;
            map = new GMap2(document.getElementById("map_canvas"));
            var center = new GLatLng(37.4419, -122.1419);
            map.setCenter(center, zoom);

            GEvent.addListener(map, "click", function(overlay,point)
            {
                if (overlay != null)
                {}
                else
                {
                    var n = boundary.length;

                    switch (cCount)
                    {
                        case 0:
                            point1 = new GMarker(point,{draggable: true});
                            map.addOverlay(point1);
                            cCount++;
                            GEvent.addListener(point1, "dragend", function()
                                {
                                    alert('P1 Dragged: '+point);
                                });

                            break;
                        case 1:
                            point2 = new GMarker(point,{draggable: true});
                            map.addOverlay(point2);
                            cCount++;
                            GEvent.addListener(point2, "dragend", function()
                                {
                                    alert('P2 Dragged: '+point);
                                });
                            break;
                        case 2:
                            map.clearOverlays();
                            cCount=0;
                            break;
                    } 
                }
            });
        }
    }

UPDATE: I have solved this problem. You see with respect to the GMarker object, the dragend,click and other events pass a GLatLng object as a paramater. So if I want to get the point then I call param.lat() or param.lng(). See code here:

                                GEvent.addListener(point2, "dragend", function(pnt)
                                {
                                    alert('P2 Dragged: '+pnt.lat()+" , "+pnt.lng());
                                    document.getElementById("point2").innerHTML="";
                                    document.getElementById("point2").innerHTML=point;
                                });
A: 

Try to add overlay and point params to yours listener functions, for example:

GEvent.addListener(point2, "dragend", function(overlay, point)
{
  alert('P2 Dragged: '+point);
});
antyrat
that doesn't seem to work. thanks for trying though.
Conor H
that's strange, can you show me your new initialization code?
antyrat
All I did was add overlay and point to the function params like you said.
Conor H
Alert message shows 37.4419, -122.1419 coords anyway? Dou you have a live demo?
antyrat
no sorry, not at the minute.Whatever I do with this code it always show those coords.
Conor H
Maybe you can paste html code to http://dpaste.com/ and give me a link, then I try to help you locally, with no suggestions.
antyrat
I have put a demo here now. http://conorhackett.com/map/I am open to any help you can offer. Thanks.
Conor H
Ok so I fixed it see updated code for explanation and code..
Conor H
+1  A: 

UPDATE: I have solved this problem. You see with respect to the GMarker object, the dragend,click and other events pass a GLatLng object as a paramater. So if I want to get the point then I call param.lat() or param.lng(). See code here:

                            GEvent.addListener(point2, "dragend", function(pnt)
                            {
                                alert('P2 Dragged: '+pnt.lat()+" , "+pnt.lng());
                                document.getElementById("point2").innerHTML="";
                                document.getElementById("point2").innerHTML=point;
                            });
Conor H