Google Maps API google.maps.event undefined

I am trying to use the Google Maps API to get the location provided by the user. To do this, I set a marker that moves based on click events. Code as such:

function initialize_google_map(div_id) {
    var map = new GMap2(document.getElementById(div_id));

    map.setCenter(new GLatLng(45, -105), 2);

    map.setUIToDefault();

    return map;
}

$(document).ready(function() {
    // configure the google maps api
    var map = initialize_google_map("map_canvas");
    var marker = google.maps.Marker({map: map, title:"Location"});
    google.maps.event.addListener(map, "click", function(evt) {
        alert("got click event");
        marker.setPosition(evt.latLng);
    });

    $(document).unload(function() {
    // unload the google map
    GUnload();
    }); 

});

The warning "received click event" never fires, and my Javascript console (Google Chrome) says the following:

Uncaught TypeError: Unable to call 'addListener' method from undefined

The API is included something like this:

<script src="http://maps.google.com/maps?file=api&v=3&sensor=true" type="text/javascript"></script>

+3
source share
2 answers

, Google Maps 3. initialize_google_map GMap2 ( 2). google.maps.Marker ( 3).

initialize_google_map, google.maps.Map.

+2

, :

$(document).ready(function() {      
     google.maps.event.addListener(map, "click", function(evt) {
            alert("got click event");
            marker.setPosition(evt.latLng);
        });
});

:

$(window).load(function(){   
    google.maps.event.addListener(map, "click", function(evt) {
            alert("got click event");
            marker.setPosition(evt.latLng);
        });
});
0

Source: https://habr.com/ru/post/1781185/


All Articles