Trying to only have one marker on the map

38 views
Skip to first unread message

Laythss

unread,
Jan 2, 2012, 2:54:50 PM1/2/12
to google-map...@googlegroups.com
I am trying to only allow one marker on the map, so if the visitors clicks on one spot first and then clicks on another spot on the map the marker dissapears from first spot and appears on the second spot. I followed the example in :
http://code.google.com/apis/maps/articles/phpsqlinfo_v3.html

i am assuming what i have to fix is in the following lines but i am not sure how to properly go about it:

google.maps.event.addListener(map, "click", function(event) {
marker = new google.maps.Marker({
position: event.latLng,
map: map
});
google.maps.event.addListener(marker, "click", function() {
infowindow.open(map, marker);
});
});

since i think the code as it is creates a new marker everytime the map is clicked

i tried :
google.maps.event.addListener(map, 'click', function(event){
marker.setPosition = event.latLng;
})

but i guess i am missing something since that made my clicks on the map go unnoticed and no marker whatsoever appears.

the page where i have the code is on:
http://www.findriyadh.com/g_addstore.php


Any help is appreciated. Thanks in advance

geoco...@gmail.com

unread,
Jan 2, 2012, 7:19:17 PM1/2/12
to Google Maps JavaScript API v3
On Jan 2, 11:54 am, Laythss <layt...@gmail.com> wrote:
> I am trying to only allow one marker on the map, so if the visitors clicks
> on one spot first and then clicks on another spot on the map the marker
> dissapears from first spot and appears on the second spot. I followed the
> example in :http://code.google.com/apis/maps/articles/phpsqlinfo_v3.html
>
> i am assuming what i have to fix is in the following lines but i am not
> sure how to properly go about it:
>
> google.maps.event.addListener(map, "click", function(event) {
> marker = new google.maps.Marker({
> position: event.latLng,
> map: map});
>
> google.maps.event.addListener(marker, "click", function() {
> infowindow.open(map, marker);
>
> });
> });
>
> since i think the code as it is creates a new marker everytime the map is
> clicked
>
> i tried :
> google.maps.event.addListener(map, 'click', function(event){
> marker.setPosition = event.latLng;
>
> })
>
> but i guess i am missing something since that made my clicks on the map go
> unnoticed and no marker whatsoever appears.

I see markers appear where I click (what browser are you using? you
might either have a browser dependency or a bad version of your page
stuck in the browser cache.

You don't have any code to remove the old marker before creating the
new one, so they keep accumulating.

-- Larry

Alan Pearce

unread,
Jan 2, 2012, 9:58:47 PM1/2/12
to Google Maps JavaScript API v3
You are creating a new marker each time someone clicks on the map. You
should only create a marker on the first click and on subsequent
clicks move it.

Hope this helps,
Alan

Laythss

unread,
Jan 3, 2012, 1:01:20 AM1/3/12
to google-map...@googlegroups.com
thanks Larry and Alan...
Alan that is what i am trying to do.. how do i go about doing that?

Laythss

unread,
Jan 3, 2012, 7:45:28 AM1/3/12
to google-map...@googlegroups.com
Thanks Alan and Larry.
Allan that is what i have been trying to do, but i think i am failing. how do only create one marker and start a new instance of a marker everytime the map is clicked?

Alan Pearce

unread,
Jan 3, 2012, 11:22:15 PM1/3/12
to Google Maps JavaScript API v3
It looks like you figured out a way to do it, however you should move
the marker click event assignment into the else where you create the
marker, you only need to do it once, otherwise you are attaching
multiple click events to the same marker.

Alan
Reply all
Reply to author
Forward
0 new messages