views:

62

answers:

3

This is kind of a newebie question, I'm trying to add information to my markers, how do I add a balloon with information to this code

map.addOverlay(new GMarker(new GLatLng(-34.8779420,-58.5514125)));

On the other hand I have a second question, when I add the balloon for what I read I'm adding a second point but this point is clickable?

A: 

EDIT: This answer is for the GMaps V3 -- which you should use if you are starting a new project - but your question implies GMaps V2.

Have you looked here: http://code.google.com/apis/maps/documentation/javascript/overlays.html#InfoWindows

And viewed the source here:
http://code.google.com/apis/maps/documentation/javascript/examples/infowindow-simple.html

This is the link that adds the infowindow / bubble:

var infowindow = new google.maps.InfoWindow({content: contentString});

Christopher Altman
You have linked to the v3 api documentation (which is the current recommended version). But his question is v2 code (GMarker and GLatLng), so these examples won't help.
Cannonade
thanks for answering @Cannonade =D, thanks for the answer to you too Altman, I did read both pages
Saikios
@Saikios No problems :). One other point on this answer. The last line of code will instantiate an info window in version three of the API, but it won't show it on the map. You will need to call the open method of the info window object to do that.
Cannonade
+1  A: 

You need to call GMarker openInfoWindowHtml to open an info window anchored to the marker object. You call this method from an event handler that you add to the click event on the GMarker object:

GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("<h1>test</h1><p>test marker content</p>");
    });

Here is an example of adding multiple markers and opening info windows for each one (source).

You second question:

You can arbitrarily add markers to your google map and then add event listeners for those markers. So, yes, your second marker can be clickable.

N.B. You are using the version two API in your example. Recently version three of the API was moved up to production status and is recommended for new sites.

Here is an example of creating an info window and opening it for a marker in version three of the API:

var infowindow = new google.maps.InfoWindow({content: "blah"});

var marker = new google.maps.Marker({
        position: point,
        map: map,
        });

google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
    });
Cannonade
What bennefits I get besides being in the last and current version?I'm not getting the balloon :( how would the code should look like?for example if I have ... map.addOverlay(new GMarker(new GLatLng(<?php echo $user['longitude'];?>,<?php echo $user['latitude'];?>)));inside a while with N results? :-S sorry for asking this @Cannonade it's driving me crazy :P
Saikios
@Saikios Version three is definitely the better alternative. The API is easier to use, performs better and now has basically the same feature set as the original. So before you get too far down the v2 rabbit hole, try starting fresh with v3.
Cannonade
@Cannonade I was trying, but is not easy on this code :P, do you know if there is any good example with more than one marker?
Saikios
@Saikios The example I linked to in my answer has multiple markers, you can check that out (n.b. version two code).
Cannonade
A: 

In answer to your first question try something like this.

var map = new GMap2(document.getElementById("map"));
var gmarkers = [];

map.addOverlay(createMarker(new GLatLng(-34.8779420,-58.5514125), "Marker1"));

function createMarker(point, name)
{
var marker = new GMarker(point);

if(name!="UserMarker")
    gmarkers.push(marker);  

GEvent.addListener(marker, 'click', function() {
var HTML_CODE= "<p>Hello</p>";
marker.openInfoWindowHtml(HTML_CODE);
});

return marker;  

}

I don't understand what you want from your second question.

Ash Burlaczenko
I read in a few forums a code that they made a function that added a point that was touchable, and if you touch it will pop up the balloon, So my question is if you can link the action to the marked point or if they are 2 different actions
Saikios