Google Maps Information Box with JS

Asked

Viewed 332 times

7

How do I display an information box when I click on Mark? I tried following the documentation, but when I click, the box appears but not the information inside it.

 //Center
    var mapProp = {
        center: new google.maps.LatLng(-14.235004, -51.92528),
        zoom: 5,
        disableDefaultUI: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    //Map 
    var map = new google.maps.Map(document.getElementById("googleMapsOne"), mapProp);

    var contentString = '<div id="content">Informação</div>';

    var infowindow = new google.maps.InfoWindow({
      content: contentString,
      maxWidth: 200
    });

    // Circle 
    var mycityCircle = new google.maps.Circle({
        center: new google.maps.LatLng(7.2964, 80.6350),
        radius: l,
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillOpacity: 0.2
    });
    mycityCircle.setMap(map);
    // Marker
    locations.map(function(val){
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(val[1], val[2]),
            icon: 'images/pin.png',
            map: map,
            animation: google.maps.Animation.BOUNCE
        });
        //marker.setMap(map)
        marker.addListener('click', function() {
          infowindow.open(map, marker);
        });
    });

I need to add to each Marker, a different text (that comes from my API), I tried to put it also inside the Map, but there it does not work

1 answer

9


Fortunately I managed to solve, for those who had the same problem I did, follows the updated code.

function initialize(locations, string){

        //Center
        var mapProp = {
            center: new google.maps.LatLng(-14.235004, -51.92528),
            zoom: 5,
            disableDefaultUI: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        //Map 
        var map = new google.maps.Map(document.getElementById("googleMapsOne"), mapProp);

        var contentString = '<div id="content">Rafael</div>';

        var infowindow = new google.maps.InfoWindow();

        locations.map(function(val){
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(val[1], val[2]),
                icon: 'images/truck.png',
                map: map,
                animation: google.maps.Animation.BOUNCE
            });
            //marker.setMap(map)
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.setContent(val[0])
              infowindow.open(map, marker);
            });
        });

    }

Browser other questions tagged

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