Mona Coder
Mona Coder

Reputation: 6316

How to add marker(s) to Google Map using jQuery

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?

Upvotes: 0

Views: 96

Answers (1)

McGarnagle
McGarnagle

Reputation: 102783

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)

Upvotes: 1

Related Questions