removeEventListener won't destroy an event

128 views
Skip to first unread message

Darren James

unread,
Nov 21, 2009, 2:59:40 PM11/21/09
to KML Developer Support - Google Earth Plug-in
I'm using the Google Earth Plugin as part of a project using Google
Maps. I'm loading data via a KML file and all is good. So I have
something running a bit like this:
http://earth-api-samples.googlecode.com/svn/trunk/demos/mapsapi/index.html

I've create http links outwith the map UI that calls a function that
moves from one location to another, and pops up an infowindow when it
arrives. Everything works well. Unfortunately when I try to remove the
'viewchangeend' event, it never seems to go away.

This is the function

// ---------------------
// Zooms to an object in ge and shows infowindow
function GEZoomer(obj, lat, lng) {

// using gmap objects within ge...
google.earth.addEventListener(ge.getView(), 'viewchangeend',
function() {GEvent.trigger(obj, "click");});

ge.getOptions().setFlyToSpeed(2);
// Get the current view
var lookAt = ge.getView().copyAsLookAt
(ge.ALTITUDE_RELATIVE_TO_GROUND);
// Set new latitude and longitude values
lookAt.setLatitude(lat);
lookAt.setLongitude(lng);

// Update the view in Google Earth
ge.getView().setAbstractView(lookAt);

// clean up event listener
google.earth.removeEventListener(ge.getView(), 'viewchangeend',
function() { GEvent.trigger(obj, "click"); });
}

My problem is any time I drag and drop the map, the event still exists
and pops open the window, even though I *thought* I'd removed it. Is
there a a problem with my use of removeEventListener?

thanks.

Andy4834

unread,
Nov 22, 2009, 9:26:50 AM11/22/09
to KML Developer Support - Google Earth Plug-in
Darren
I think that your use of removeEventListener needs to be modified - it
should be passed the function that you actually created. In
addEventListener you create an anonymous function which can never be
referenced again and in removeEventListener you create another
anonymous function. So you need to create the function before the
addEventListener and pass the same variable referencing the function
to both addEventListener and removeEventListener.
Hope this helps
Andy

On Nov 21, 7:59 pm, Darren James wrote:
> I'm using the Google Earth Plugin as part of a project using Google
> Maps. I'm loading data via a KML file and all is good. So I have
> something running a bit like this:http://earth-api-samples.googlecode.com/svn/trunk/demos/mapsapi/index...

Matthew J Reines

unread,
Nov 22, 2009, 5:50:34 PM11/22/09
to KML Developer Support - Google Earth Plug-in
Darren I believe Andy is right try this:

function pullTrigger()
{
GEvent.trigger(obj, "click");
}

function GEZoomer(obj, lat, lng) {
// using gmap objects within ge...
google.earth.addEventListener(ge.getView(), 'viewchangeend',
pullTrigger;
ge.getOptions().setFlyToSpeed(2);
// Get the current view
var lookAt = ge.getView().copyAsLookAt
(ge.ALTITUDE_RELATIVE_TO_GROUND);
// Set new latitude and longitude values
lookAt.setLatitude(lat);
lookAt.setLongitude(lng);
// Update the view in Google Earth
ge.getView().setAbstractView(lookAt);
// clean up event listener
google.earth.removeEventListener(ge.getView(), 'viewchangeend',
pullTrigger);
}


May not be exact correct code but you get the picture.

Matthew J Reines

unread,
Nov 22, 2009, 5:52:48 PM11/22/09
to KML Developer Support - Google Earth Plug-in
One other thing Darren, in this methodology you should remove the
event listener in the 'pullTrigger' event, or it would be removed
prior to ever being fired by the viewChangeEnd event.

On Nov 21, 12:59 pm, Darren James wrote:
> I'm using the Google Earth Plugin as part of a project using Google
> Maps. I'm loading data via a KML file and all is good. So I have
> something running a bit like this:http://earth-api-samples.googlecode.com/svn/trunk/demos/mapsapi/index...

Darren James

unread,
Nov 23, 2009, 6:49:16 AM11/23/09
to KML Developer Support - Google Earth Plug-in
Many, many thanks Andy and Matthew. I had a feeling I was missing a
trick somewhere. All sorted now :)

If you are interested, the demo I was working on is available at
http://is.gd/51Ko6 Be aware it plots around 30,000 lat/lng coords from
an unoptimised KML file so IE struggles a bit. FF and Chrome are fine
though.

thanks once again!

fraser (Earth API Guru)

unread,
Nov 24, 2009, 10:28:19 AM11/24/09
to KML Developer Support - Google Earth Plug-in
Hi,


Just to point out this is covered in the documentation in a couple of
places.

"You must pass in the exact same function object as was passed to
addEventListener. If you are using an anonymous function callback, it
will need to be refactored into its own variable."

http://code.google.com/apis/earth/documentation/events.html#removing_event_listeners
http://code.google.com/apis/earth/documentation/reference/google_earth_namespace.html#4367d554eb492adcafa52925ddbf0c71

Cheers,

F.

On Nov 23, 11:49 am, Darren James wrote:
> Many, many thanks Andy and Matthew. I had a feeling I was missing a
> trick somewhere. All sorted now :)
>
> If you are interested, the demo I was working on is available athttp://is.gd/51Ko6Be aware it plots around 30,000 lat/lng coords from

Andy4834

unread,
Nov 24, 2009, 3:55:17 PM11/24/09
to KML Developer Support - Google Earth Plug-in
Fraser
Thanks for the references. I'm using your GEPluginCtrls components in
my c# application (by theway really useful, thanks) including
addEventListener and removeEventListener. For the reasons we have
discussed above I altered the c# code to be

/// <summary>
/// Wrapper for the the google.earth.addEventListener method
/// </summary>
/// <param name="feature">The target object</param>
/// <param name="action">The event Id</param>
/// <returns>Object containing javascript function</returns>
public object AddEventListener(object feature, string action)
{
return this.InvokeJavascript(
"jsAddEventListener",
new object[] { feature, action });
}

/// <summary>
/// Wrapper for the the google.earth.removeEventListener
method
/// </summary>
/// <param name="feature">The target kml object</param>
/// <param name="action">The event Id</param>
/// <param name="eventFunction">Event function to remove</
param>
public void RemoveEventListener(object feature, string action,
object eventFunction)
{
this.InvokeJavascript(
"jsRemoveEventListener",
new object[] { feature, action, eventFunction});
}

with corresponding javascript

var jsAddEventListener = function(feature, action)
{
if (ge)
{
var eventFunction = function(kmlEvent)
{
if (kmlEvent)
{
application.KmlEventCallBack(kmlEvent,
action);
}
}
google.earth.addEventListener(feature, action,
eventFunction);
return eventFunction;
}
else
{
return null
}
}

var jsRemoveEventListener = function(feature, action,
eventFunction)
{
if (ge)
{
google.earth.removeEventListener(feature, action,
eventFunction);
}
}

so that the event function can be removed thanks to the function
closure in javascript. Perhaps this may be useful to those who are
developing mixed c# and js applications - comments welcome.
Regards
Andy

On Nov 24, 3:28 pm, fraser (Earth API Guru) wrote:
> Hi,
>
> Just to point out this is covered in the documentation in a couple of
> places.
>
> "You must pass in the exact same function object as was passed to
> addEventListener. If you are using an anonymous function callback, it
> will need to be refactored into its own variable."
>
> http://code.google.com/apis/earth/documentation/events.html#removing_...http://code.google.com/apis/earth/documentation/reference/google_eart...
>
> Cheers,
>
> F.
>
> On Nov 23, 11:49 am, Darren James wrote:
>
>
>
> > Many, many thanks Andy and Matthew. I had a feeling I was missing a
> > trick somewhere. All sorted now :)
>
> > If you are interested, the demo I was working on is available athttp://is.gd/51Ko6Beaware it plots around 30,000 lat/lng coords from
> > > > thanks.- Hide quoted text -
>
> - Show quoted text -

fraser (Earth API Guru)

unread,
Nov 27, 2009, 6:05:33 AM11/27/09
to KML Developer Support - Google Earth Plug-in
Thanks Andy,

That looks great but I think you have an old copy of the controls as
events are handled slightly differently to that now...If you have
anything else like this though I would encorrage you to post it on the
issues page for the project.
http://code.google.com/p/winforms-geplugin-control-library/issues/list

Thanks,

F.
> >http://code.google.com/apis/earth/documentation/events.html#removing_......
>
> > Cheers,
>
> > F.
>
> > On Nov 23, 11:49 am, Darren James wrote:
>
> > > Many, many thanks Andy and Matthew. I had a feeling I was missing a
> > > trick somewhere. All sorted now :)
>
> > > If you are interested, the demo I was working on is available athttp://is.gd/51Ko6Beawareit plots around 30,000 lat/lng coords from
Reply all
Reply to author
Forward
0 new messages