Markers array

1,047 views
Skip to first unread message

MrUpsidown

unread,
Jun 29, 2011, 6:21:31 AM6/29/11
to Google Maps JavaScript API v3
Hi

Within a function, I create several markers and I need to assign them
a specific ID.

var marker = new google.maps.Marker({
position: latlng,
icon: image,
map: map,
title: name,
draggable: true
});

marker.myid = markerid;

gmarkers.push(marker);

Now I have another function from which I want to 'click' a link that
contains the marker id and fire a click event

function spotClick(markerId) {

google.maps.event.trigger(gmarkers[markerId],"click");

}

This obviously does not work. How am I supposed to refer to marker by
the ID I assigned to it earlier?

Thanks in advance!

Mike Dolbow

unread,
Jun 29, 2011, 9:23:26 AM6/29/11
to google-map...@googlegroups.com
Can you individually assign IDs with the variable you use to define each marker? So instead of
var marker = new google.maps.Marker...
do
var marker01 = new google.maps.Marker...
?

Would that work?

Pil

unread,
Jun 29, 2011, 9:24:32 AM6/29/11
to Google Maps JavaScript API v3
This should work (untested pseudocode):

function createMarker(id) {

var marker = new google.maps.Marker({
position: latlng,
icon: image,
map: map,
title: name,
draggable: true,
id: id
});
gmarkers.push(marker);
}

Now it should be possible to access every marker in gmarkers array by
its id:

for (var i = 0; i < gmarkers.length; i++) {
if (gmarkers[i].id == someid) {
// do soemthing with your specific marker

Arnaud

unread,
Jun 29, 2011, 11:08:06 AM6/29/11
to google-map...@googlegroups.com
Ok thank you, I will try that. Sounds good.

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


Richard Quadling

unread,
Jun 30, 2011, 5:53:04 AM6/30/11
to google-map...@googlegroups.com

I have a similar need. I'd like to be able to open a map, add a load
of markers (the position of vehicles performing certain duties).

I have a mechanism to periodically get the lastest data and to update
the positions of the vehicles.

Based upon the example code that Pil provided, I think I can get that
bit all working.

I can handle a new vehicle (vehicles go on and off jobs, so as a
vehicle goes on job it appears on the map).

The part I can't seem to do is to deal with a vehicle that goes off job.

I think ...

var marker = new google.maps.Marker(...);

could be ...

var singleMarker = {
id: 'this_markers_id',
lastUpdated : Date.now(),
marker : new google.maps.Marker(...)
};

myMarkers.push(singleMarker);

Ideally, I'd like to use the ID as a key (like in an associative array).

So, maybe ...

myMarkers['this_markers_id'] = singleMarker;

Does this sound feasible?

--
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

Pil

unread,
Jun 30, 2011, 6:28:27 AM6/30/11
to Google Maps JavaScript API v3


On Jun 30, 11:53 am, Richard Quadling <rquadl...@gmail.com> wrote:

> So, maybe ...
>
> myMarkers['this_markers_id'] = singleMarker;


You cannot compare a property of a marker with the marker object (or
define a marker property as a marker).
There is also no comparison operator to compare complex objects, you'd
have to write a compare function yourself.

When the ID properties are unique you are on the safe side to compare
them.

Ben Appleton

unread,
Jun 30, 2011, 7:19:50 AM6/30/11
to google-map...@googlegroups.com

You can do this by defining a custom .toString method, like:

singleMarker.toString = function() {
  return this.id;
};

Then you can write:

myMarkers[singleMarker] = singleMarker;

- Ben

Android brevity

Reply all
Reply to author
Forward
0 new messages