Julius Caesar's great-great-granddaughter (zyada) wrote in javascript,
Julius Caesar's great-great-granddaughter
zyada
javascript

Knowing just enough to be dangerous

I have a php script that is generating a page with Google maps on it. I've extracted the html and placed it here.


The code I'm trying to figure out is in the initialize function. When the user right clicks on the map, it places a marker there. I've got that working.

What I want to do next is if the user right clicks on another spot, the first marker is removed and a new marker is put into place. What I've got right now doesn't look like it realizes that a marker has already been created.

Here's the code I'm working on:

function initialize() {
if (GBrowserIsCompatible()) {

var map0 = new GMap2(document.getElementById("map_canvas_0"));
map0.setCenter(new GLatLng(31.25044240, -99.25060610), 6);
map0.setUIToDefault();
GEvent.addListener(map0,"singlerightclick", function(point, src, overlay){
var cur_ll = map0.fromContainerPixelToLatLng(point);
if (typeof map_marker0 != "undefined" )
{
removeOverlay(map_marker0)
alert("there's already one marker out there!");
}
var map_marker0 = map0.addOverlay(new GMarker(cur_ll, {draggable: true}));
} );


Also, the listener function for the event is inline because that's how their example is - is it possible to put this function outside of the addListener call?
  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

  • 1 comment