How do I add bookmarks to the google map when I click it? - Javascript

Asked

Viewed 1,639 times

0

I would like to know how to add markers to the map by clicking on a point on google maps. Actually create/add the marker "manually" I already know, what I would really like is to trigger the function of adding the marker by passing the latitudes and longitudes as 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 someone help me with this?

2 answers

1

It worked here, you can do putting 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");
});

1


In fact what you need is just control the user click. At the time of the click you already get that data.

Add that Listener at map initialization:

mapa.addListener('click', function (e) {
    CarregarEnderecoPorLatLng(e.latLng.lat(), e.latLng.lng());
}
  • It worked right here buddy. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.