event.addListener - Marker is undefined

2,039 views
Skip to first unread message

Harald Effenberg

unread,
Jul 9, 2010, 5:24:42 AM7/9/10
to Google Maps JavaScript API v3
Hi!

I have a problem in line 168 on
http://www.effenberg.de/maps-v3-e.htm

"marker is not defined"

The code is

google.maps.event.addListener(marker, 'click', function(event)
{
marker.setMap(null);
});

But there is always a marker if I remove those lines, no matter if it
is at it's default place on
http://www.effenberg.de/maps-v3.htm
or at a variable place on i.e.
http://www.effenberg.de/maps-v3.htm?mainstreet

The markers are in both cases created before the eventlistener is
added, so how can "marker" be undefined?
How can I remove a marker by a mouseclick on it?

TIA and best regards
Harry

Chad Killingsworth

unread,
Jul 9, 2010, 7:42:35 AM7/9/10
to Google Maps JavaScript API v3
It is a scope problem. Your marker is only defined in your geocode
result functions, but you are trying to assign the click event outside
of those functions. To fix the issue, move your click event handler
into your geocode result functions.

Chad Killingsworth

On Jul 9, 4:24 am, Harald Effenberg <w...@effenberg.de> wrote:
> Hi!
>
> I have a problem in line 168 onhttp://www.effenberg.de/maps-v3-e.htm
>
> "marker is not defined"
>
> The code is
>
>     google.maps.event.addListener(marker, 'click', function(event)
>     {
>     marker.setMap(null);
>     });
>
> But there is always a marker if I remove those lines, no matter if it
> is at it's default place onhttp://www.effenberg.de/maps-v3.htm
> or at a variable place on i.e.http://www.effenberg.de/maps-v3.htm?mainstreet

Grégoire Colbert

unread,
Jul 9, 2010, 6:14:24 AM7/9/10
to Google Maps JavaScript API v3
Hi Harry,

Your "var marker" is done inside a "if" block. You need to declare
your variable at the top of the initialize function if you want
"marker" to be visible below the "if" block. So write :

var marker;

if (geocoder) {
(...)
marker = new google.maps.Marker({
map: map,
position: zentrum,
title: adresse
});
(...)
}

google.maps.event.addListener(marker, 'click', function(event) {
marker.setMap(null);
});

Hope this helps.

Grégoire

On 9 juil, 11:24, Harald Effenberg <w...@effenberg.de> wrote:
> Hi!
>
> I have a problem in line 168 onhttp://www.effenberg.de/maps-v3-e.htm
>
> "marker is not defined"
>
> The code is
>
>     google.maps.event.addListener(marker, 'click', function(event)
>     {
>     marker.setMap(null);
>     });
>
> But there is always a marker if I remove those lines, no matter if it
> is at it's default place onhttp://www.effenberg.de/maps-v3.htm
> or at a variable place on i.e.http://www.effenberg.de/maps-v3.htm?mainstreet

Pil

unread,
Jul 10, 2010, 2:55:17 AM7/10/10
to Google Maps JavaScript API v3


On Jul 9, 12:14 pm, Grégoire Colbert <gcolb...@gmail.com> wrote:

>   var marker;
>
>   if (geocoder) {
>     (...)
>     marker = new google.maps.Marker({
>                                 map: map,
>                                 position: zentrum,
>                                 title: adresse
>                             });
>     (...)

Thats really not a good idea, because marker would be indeed declared
when the if block would not be executed, but it also remains undefined
in this case.


Reply all
Reply to author
Forward
0 new messages