Changing marker icon

308 views
Skip to first unread message

gdp

unread,
Jul 18, 2011, 7:55:12 AM7/18/11
to Google Maps JavaScript API v3
Hi - the function below is called to create a set of markers on a map.
My problem is that within the marker click listener I am trying to
change the icon image. What is in fact happening is that the last
marker to be added to the map ends up showing my 'special' icon as
soon as it is drawn and the click event does not change the icon for
any clicked marker. The 2 alerts show the problem - the first alert
shows the correct marker id value onclick, the second alert always
shows the id of the last marker added to the map and this is the
marker that is given the 'special' image. So I guess my question is
how to do get the setIcon method to act on the marker identified in
the first alert.

Hope that makes a bit of sense to someone. Thanks for any help given
as usual.


function createMarker(markerData) {
loc = new google.maps.LatLng(markerData.lat, markerData.lng);

marker = new google.maps.Marker({
position: loc,
map: map,
icon: image,
html: content,
id: markerData.id
});

google.maps.event.addListener(marker, 'click', function() {
$("#markerDetail" + markerData.id).trigger("click");
$('#markerDetailCont').scrollTo("#markerDetail" + markerData.id,
{speed:1000});

map.panTo(new google.maps.LatLng(markerData.lat, markerData.lng));
marker.setIcon('/images/site/marker_w.png');
alert(markerData.id);
alert(marker.id);
});

return marker;
}

geoco...@gmail.com

unread,
Jul 18, 2011, 8:22:47 AM7/18/11
to Google Maps JavaScript API v3
On Jul 18, 7:55 am, gdp <tidy...@googlemail.com> wrote:
> Hi - the function below is called to create a set of markers on a map.
> My problem is that within the marker click listener I am trying to
> change the icon image. What is in fact happening is that the last
> marker to be added to the map ends up showing my 'special' icon as
> soon as it is drawn and the click event does not change the icon for
> any clicked marker. The 2 alerts show the problem - the first alert
> shows the correct marker id value onclick, the second alert always
> shows the id of the last marker added to the map and this is the
> marker that is given the 'special' image. So I guess my question is
> how to do get the setIcon method to act on the marker identified in
> the first alert.
>
> Hope that makes a bit of sense to someone. Thanks for any help given
> as usual.

Where is the link to a map that shows the problem?

-- Larry

gdp

unread,
Jul 18, 2011, 8:36:49 AM7/18/11
to Google Maps JavaScript API v3
unfortunately hidden in a corporate internet :(



On Jul 18, 1:22 pm, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Jul 18, 7:55 am, gdp <tidy...@googlemail.com> wrote:
>
> > Hi - the function below is called to create a set of markers on a map.
> > My problem is that within themarkerclick listener I am trying to
> > change theiconimage. What is in fact happening is that the last
> >markerto be added to the map ends up showing my 'special'iconas
> > soon as it is drawn and the click event does not change theiconfor
> > any clickedmarker. The 2 alerts show the problem - the first alert
> > shows the correctmarkerid value onclick, the second alert always
> > shows the id of the lastmarkeradded to the map and this is the
> >markerthat is given the 'special' image. So I guess my question is
> > how to do get the setIcon method to act on themarkeridentified in

geoco...@gmail.com

unread,
Jul 18, 2011, 8:44:39 AM7/18/11
to Google Maps JavaScript API v3
On Jul 18, 8:36 am, gdp <tidy...@googlemail.com> wrote:
> unfortunately hidden in a corporate internet :(

You can fix that, I can't.

http://groups.google.com/group/Google-Maps-API/web/why-including-a-link-is-critical

-- Larry

Florian Behr

unread,
Jul 18, 2011, 8:39:32 AM7/18/11
to google-map...@googlegroups.com
Then Read the Terms of Service.
Gmaps API is for public use only.

2011/7/18 gdp <tid...@googlemail.com>
--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


gdp

unread,
Jul 18, 2011, 2:38:26 PM7/18/11
to Google Maps JavaScript API v3
Thanks for such friendly responses - this project is on a company
intranet, its not a profit making thing - just me messing about trying
to provide my co-workers with a tool - it is impossible for me to make
it public without setting up a duplicate site which would involve me
having to make it all anonoymous etc which is too much effort -
particularly seeing as the issue is not the end of the world the that
the two reponses so far are unencouraging, i have found that usually
on these groups if somebody is more interested in being a smart arse,
self righteous or unpleasant they also would be incapable of actually
giving an answer to the question




On Jul 18, 1:39 pm, Florian Behr <m...@florianbehr.de> wrote:
> Then Read the Terms of Service.
> Gmaps API is for public use only.
>
> 2011/7/18 gdp <tidy...@googlemail.com>

Andrew Leach

unread,
Jul 18, 2011, 2:52:11 PM7/18/11
to google-map...@googlegroups.com
On 18 July 2011 19:38, gdp <tid...@googlemail.com> wrote:
> Thanks for such friendly responses - this project is on a company
> intranet,

One problem is that not many people want to assist with breaking the
Terms of Use. They are restrictive enough already; we don't really
want Google to make them even more so.

The free API is not for company intranets: you need to buy a Premier
licence, and that comes with free support from Google themselves.
Well, it's not actually free, of course; you pay for it in the fee.
But it helps keep the free API free for those who can comply with the
Terms of Service, and lightens the load on members here because you
can ask Google -- thus freeing expertise here for those who can't
afford a licence and who will play by the rules.

That's one reason, or maybe two. Another is that we can't see what
you're doing on your network, so if you want help then you need to
make your map available to those you're asking for help. Code listings
aren't really useful in that regard.

geoco...@gmail.com

unread,
Jul 18, 2011, 4:17:33 PM7/18/11
to Google Maps JavaScript API v3
On Jul 18, 2:38 pm, gdp <tidy...@googlemail.com> wrote:
> Thanks for such friendly responses - this project is on a company
> intranet, its not a profit making thing - just me messing about trying
> to provide my co-workers with a tool

That is against the terms of use unless you have a Premier license.

> - it is impossible for me to make
> it public without setting up a duplicate site which would involve me
> having to make it all anonoymous etc which is too much effort -
> particularly seeing as the issue is not the end of the world the that
> the two reponses so far are unencouraging, i have found that usually
> on these groups if somebody is more interested in being a smart arse,
> self righteous or unpleasant they also would be incapable of actually
> giving an answer to the question

I have lots of public examples that demonstrate custom icons. I am
sure I could make a map that meets your requirement, but you haven't
made it easy. A link to your map that shows how far you have gotten
gives me something to start with. Guessing what people want tends to
end up wasting my time, copying and pasting from code posted to the
group tends to introduce errors. My take is you are asking for free
help, help us to help you. A good start is following the posting
guidelines.

-- Larry
Reply all
Reply to author
Forward
0 new messages