0

I am trying to add marker(s) to Google Map through jQuery method at this demo and this is the code which I am using:

var map;
$(document).ready(function () {
    var latlng = new google.maps.LatLng(49.241943, -122.889318);
    var myOptions = {
        zoom: 12,
        center: latlng,
        disableDefaultUI: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

google.maps.event.addListener(map, 'click', function (event) {
        marker = new google.maps.Marker({
            position: event.latLng,
            map: map,
            draggable: true,
            title: title
        });
        marker.setMap(map);
    });
});

As you can see I am using this snippet to generate markers on the map

google.maps.event.addListener(map, 'click', function (event) {
            marker = new google.maps.Marker({
                position: event.latLng,
                map: map,
                draggable: true,
                title: title
            });
            marker.setMap(map);
        }); 

but it is not adding any Marker(s) to the map! What am I doing wrong?

1 Answer 1

1

You just need to define the variable "title". (Firebug is your friend.)

var cnt = 0;
marker = new google.maps.Marker({
            position: event.latLng,
            map: map,
            draggable: true,
            title: "test title #" + ++cnt
        });

(Fiddle)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks McGarnagle, Can you please also let me know how I can add a sequence of numbers for title instead of having a static string? once again thanks
@MonaCoder sure, just define a variable and increment each time you create a marker. See my edit above.
I really appreciate your comment and help. I am going to work on registering each markers lat and long values and displaying them on a table
I am not sure how hard it is gonna be but I will my best :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.