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 r开发者_运维知识库espect 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;
});
Try to add overlay
and point
params to yours listener functions, for example:
GEvent.addListener(point2, "dragend", function(overlay, point)
{
alert('P2 Dragged: '+point);
});
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;
});
精彩评论