How do I add bookmarks to the google map by clicking on it? - JavaScript

0

I'd like to know how to add bookmarks to the map by clicking on a point in google maps. Actually creating / adding the marker "manually" I already know, what I really wanted was to trigger the function of adding the marker by passing the latitudes and longitudes as a parameter when clicking on the map. Here's what I've done so far:

var mapa;

criarMapa();

function criarMapa() {
    var latLng = {lat: -8.063074, lng: -34.871129};
    mapa = new google.maps.Map(document.getElementById('mapa'), {
        zoom: 18,
        center: latLng
    });

    var marcador = new google.maps.Marker({
        position: latLng,
        map: mapa

    });
}

function adicionarMarcador(latitude, longitude) {

    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(latitude, longitude),
        map: mapa
    });

}

Could anyone help me with this?

    
asked by anonymous 28.07.2017 / 22:56

2 answers

1

Actually what you need is just to control the click of the user. At time click you already get this data.

Adds this listener at map initialization:

mapa.addListener('click', function (e) {
    CarregarEnderecoPorLatLng(e.latLng.lat(), e.latLng.lng());
}
    
28.07.2017 / 23:04
1

It worked here, you can do by placing the event also by the marked point

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(latitude, longitude),
    map: mapa
});

marker.addListener('click', function() {
     console.log("pegou click no marcador");
});
    
09.01.2018 / 13:52