Search code examples
javascriptgoogle-maps-api-3infowindow

HTML5 geolocation info window not working


I need to create info-window out of this code, I'm lacking of application logic and totally new to programming i use this open-source code

This code add users to the map along with their marker

    function loadMarkers(data) {
    for(key in data) {
        var user = data[key];
        xmartlabschat.addUser(user);
        markers[key] = getMarker(user.lat, user.lng, user.name);
    }
}

I tried to use the basic code for info-window

var infoWindow = new google.maps.InfoWindow(user.name);
markers[key].addListener('click', function(){
        infoWindowMe.open(map,markers[key);

However this code turns all marker into same name with the user instead of different names since im clicking other users marker

Please Help Here the screenshot I took


Solution

  • You can try this. Hope this helps.

    var locations = [
    ['Location name', LAT, LON],
    ['...', ..., ...],
    //...
    ];
    
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 8,
        center: new google.maps.LatLng(LAT, LON),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var infowindow = new google.maps.InfoWindow();
    
    var marker, i; 
    for (i = 0; i < locations.length; i++) {
        var lat = locations[i][1];
        var lng = locations[i][2];
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, lng),
            map: map,
        }); // generates all the markers
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                zIndex: 1000;
                infowindow.setContent(locations[i][0]); // replace locations[i][0] with which data you want to show
                infowindow.open(map, marker);
            }
        })(marker, i)); // generates corresponding info-window
    }