The way you first described was a bit redunandant in order to accomplish
what the person was doing.  From your example it was more or less used to
just set the title which you could have done by var marker = new
google.maps.Marker({"position": position, "title": name, "map":map});

What this method is normally done is so that you can write one method,
createMarker, for calling several times for creating a handful of markers
when you have to call it multiple times or are creating markers dynamically.

In your createMarker you could further add the listeners you want for the
markers within the createMarker function as well.  What I would typically do
if I was using your example is do something like this:

Create an array myMarkers to store your markers in:
myMarkers = [];        or myMarkers = new Array;

Do the same for your listeners for your markers:
myMarkerListeners = [];

Then when I create a marker I would do something like
myMarkers.push(createMarker(position, id));

Then your createMarker would create your marker
marker = new google.maps.Marker({"position":position, "title":name,
"map":map});
myMarkerListeners.push(google.maps.events.addListener(marker, "click",
function() { Your function goes here });


Then you now have a function to call each time you want to create a marker
and it also creates a listener for your marker.  That is assuming you want
to create a marker with the same options and same listener each time.  You
just feed it your position and your new title and it creates the marker with
that position and title and the click event listener for that marker.

You also now have them stored in arrays so you can remove individual
markers, handle the individual marker, and handle the listeners for each
marker.   You just call the myMarkers[MarkerID] for the marker you want to
do something with.

Another tip I use, I like to have id's of my markers be unique numbers that
have some relationship to the marker itself so I can easily know which
marker I want to handle.  Instead of a straight up myMarkers.push, which
adds a marker to the last location, you can do myMarkers[MarkerID] = to
accomplish this.  That way if you know the ID of the marker you want to
manipulate you know exactly where in the myMarkers array it is located at.

That help?

On Thu, Jun 24, 2010 at 4:02 PM, 3D-kreativ <[email protected]> wrote:

> Instead of this:
> var position = new google.maps.LatLng(56.8848, 14.7730);
> var marker = createMarker(position,'Plats 1')
>
> I could do this:
> var position = new google.maps.LatLng(56.8848, 14.7730);
> createMarker(position,'Plats 1')
>
> So I don't se the point with var marker?! Or is it for the last part
> of the code to create the infoWindows?
>
>    google.maps.event.addListener(marker, 'click', function() {
>        infowindow.setContent(contentString);
>        infowindow.open(map,marker);
>        });
>
> On 24 Juni, 22:37, Nathan Raley <[email protected]> wrote:
> > It is basically calling the create marker function to assign the name as
> a
> > title.
> >
> > There are several ways of doing this, this is one of those.
> >
> > Basically you declare marker .
> >
> > You then declare marker as being equal to what you are doing in your
> > createMarker function.
> >
> > In the createMarker function you are creating a new google.maps.Marker
> with
> > the options defined in the function.
> >
> > Marker is now the resulting marker that was created in the createMarker
> > function.
> >
> > That make sense?
> >
> > On Thu, Jun 24, 2010 at 2:43 PM, 3D-kreativ <[email protected]> wrote:
> > > Hi!
> >
> > > I found some code that I find useful, but I don't understand it 100%.
> >
> > > var marker = createMarker(position,'Plats 1')
> >
> > > Could someone tell me how this works? First is a declaration of the
> > > variable marker, the next part call a function with some value. But
> > > then, why this combination? whats the point? And in the function
> > > below, var marker is declared once again and is old value gone or? How
> > > does it work? Like to know what i'm doing! Thanks!
> >
> > > // place markers
> > > var position = new google.maps.LatLng(56.8848, 14.7730);
> > > var marker = createMarker(position,'Plats 1')
> >
> > > var position = new google.maps.LatLng(57.1719, 14.3701);
> > > var marker = createMarker(position,'Plats 2');
> >
> > > function createMarker(latlng, name) {
> > >    var marker = new google.maps.Marker({
> > >        position: latlng,
> > >        map: map,
> > >        title: name
> > >        });
> > > }
> >
> > > --
> > > 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
> > > [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<google-maps-js-api-v3%[email protected]>
> <google-maps-js-api-v3%[email protected]<google-maps-js-api-v3%[email protected]>
> >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/google-maps-js-api-v3?hl=en.
>
> --
> 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
> [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-maps-js-api-v3%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-maps-js-api-v3?hl=en.
>
>

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Reply via email to