Is there a simple way to get an icons url?

579 views
Skip to first unread message

JoshN

unread,
Nov 30, 2010, 9:38:34 PM11/30/10
to google-map...@googlegroups.com
I was trying to figure out how to find the url of a markers icon. I had been using a private property before, but realized my error and now I'm curious if there is an accessor I should be using. In the meantime, I have been using this, which relies on prototypejs:

google.maps.Marker.prototype.getIconUrl = function(){
    var strs = $H(this.getIcon()).find(String);
    re = /\.png|\.gif|\.jpg/;
    for(var i=0;i<strs.length;i++){
        if(re.match(strs[i])){
            return strs[i];
        }
    }
}

I'm sure there is a much better implementation, but I'm hoping this is a temp solution.

Any help is appreciated.

Chris Broadfoot

unread,
Dec 1, 2010, 2:22:11 AM12/1/10
to google-map...@googlegroups.com
No, there isn't.

What was wrong with your approach of using a private property?

--
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.

Message has been deleted

JoshN

unread,
Dec 1, 2010, 3:12:45 AM12/1/10
to Google Maps JavaScript API v3
**Notice, I had a typo in my last message and wound up removing it to
avoid leaving a bad snippet up.**

My earliest solution of using the private property broke with the most
recent update to the API. Mainly, I was hoping that since url is a
keyword arg for MarkerImage that there would be a getter (or a public
property) for MarkerImage.url--I'm using markers in bounds to show a
list of icons and titles.

I'm probably just splitting hairs on performance, but I feel odd using
a RegExp for this sort of problem.

In case someone else needs to get this property, here is the solution
I settled on:

google.maps.MarkerImage.prototype.getUrl = function(){
var re = /\.png|\.jpg|\.gif/;
for(var p in this){
if(re.test(this[p])){
return this[p];
}
}
return
}

Thanks for your attention.
> > google-maps-js-a...@googlegroups.com<google-maps-js-api-v3%2B unsub...@googlegroups.com>
> > .

Chris Broadfoot

unread,
Dec 1, 2010, 7:00:26 AM12/1/10
to google-map...@googlegroups.com
Right. I thought you meant a property of your own.

Why not store it in the Marker after construction, like this?

var url = getUrlForMarker(); // i assume you have some sort of url that changes...
var marker = new google.maps.Marker({
  icon: url
});
marker.iconUrl_ = url;

then you can access the 'iconUrl_' property wherever you want.

Alternatively (and probably better), store it as a MVC property with set and get...

marker.set('iconUrl', url);

then you can marker.get('iconUrl') later.

Chris

To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.

JoshN

unread,
Dec 2, 2010, 1:38:16 AM12/2/10
to Google Maps JavaScript API v3
I don't know why I wasn't thinking of this before; must need better
coffee.

Thanks.
> > > > google-maps-js-a...@googlegroups.com<google-maps-js-api-v3%2B unsub...@googlegroups.com><google-maps-js-api-v3%2B
Reply all
Reply to author
Forward
0 new messages