Anastasie Laurent
Anastasie Laurent

Reputation: 1179

google maps add marker on click javascript v3

This is my code

function initialize() {
        var mapOptions = {
            center: new google.maps.LatLng(-34.397, 150.644),
            zoom: 8
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"),
            mapOptions);
        //map.on('click', function(){});
        google.maps.event.addListener(map, 'click', function(event) {

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

    }
    google.maps.event.addDomListener(window, 'load', initialize);

when I click on the map nothing happens. So, I tried to make alter like this:

google.maps.event.addListener(map, 'click', function(event) {
    alter("here");
                var marker = new google.maps.Marker({
                    position: event.latlng,
                    map: map
                });
                map.setCenter(event.latlng);
            });

The alter works. so the function is being executed but the marker is not being added, could you help please? many thanks

Edit

I tried to do this:

 alert(event.latlng);

and I got undefined

Upvotes: 1

Views: 618

Answers (1)

K K
K K

Reputation: 18109

I think you are using incorrect variable. It should be event.latLng instead of event.latlng. Note the uppercase L in variable name.

Demo: http://jsfiddle.net/lotusgodkk/x8dSP/3578/

position: event.latLng,

Upvotes: 2

Related Questions