0

I am building a GoogleMap with multiple markers with infowindows attached. I have tried to build this into a re-usable Javascript Class using prototypes but my infowindows will not open (the markers show up fine).

If I test console.log within the click event listener it triggers, but the infowindows themselves refuse to open.

Markers.json is just a JSON object which returns a list of data to be looped through.

JS Fiddle

I am initiating the map using:

$(document).ready(function(){
    var customMap = new CustomMap('mapitems');
    customMap.init();
    customMap.setMarkers();
});

My Javascript:

function CustomMap(mapId, defaultLat, defaultLng, defaultZoom)
{
    this.map = null;
    this.markerBounds = null;
    this.$mapContainer = document.getElementById(mapId);
    this.defaultLat = (defaultLat ? defaultLat : '-36.848460');
    this.defaultLng = (defaultLng ? defaultLng : '174.763332');
    this.defaultZoom = (defaultZoom ? defaultZoom : 15);
}

CustomMap.prototype.init = function() {
    var position = new google.maps.LatLng(this.defaultLat, this.defaultLng);
    this.map = new google.maps.Map(this.$mapContainer, {
        scrollwheel: true,
        mapTypeId: 'roadmap',
        zoom: this.defaultZoom,
        position: position
    });
    this.markerBounds = new google.maps.LatLngBounds();
};

CustomMap.prototype.setMarkers = function() {
    var that = this;
    $.get('/locations.json', null, function(locations) {
        for (var i = 0; i < locations.length; i++) {
            that.processMarker(locations[i]);
        }
        that.map.fitBounds(that.markerBounds);
    }, 'json');
};

CustomMap.prototype.processMarker = function(data) {
    if (data.lat && data.lng) {
        var position = new google.maps.LatLng(data.lat, data.lng);
        this.markerBounds.extend(position);
        var marker = this.getMarker(position);
        this.setInfoWindow(marker, data);
    }
};

CustomMap.prototype.getMarker = function(position) {
    return new google.maps.Marker({
        map: this.map,
        draggable:false,
        position: position
    });
};

CustomMap.prototype.setInfoWindow = function(marker, data) {
    var that = this;
    marker.infoWindow = new google.maps.InfoWindow({
        content: that.buildInfoHtml(data)
    });
    google.maps.event.addListener(marker, 'click', function() {
        marker.infoWindow.open(that.map, marker);
    });
};

CustomMap.prototype.buildInfoHtml = function(data) {
    var html = '';
    html += '<strong>' + data.title + '</strong><br>';
    if (data.address.length > 0)
        html += data.address + '<br>';
    if (data.freephone.length > 0)
        html += 'Freephone: ' + data.freephone + '<br>';
    if (data.depotPhone.length > 0)
        html += 'Depot Phone: ' + data.depotPhone + '<br>';
    if (data.hours.length > 0)
        html += 'Depot Hours: ' + data.hours + '<br>';

    return html;
};
geocodezip
  • 158,664
  • 13
  • 220
  • 245
Josh
  • 847
  • 9
  • 17
  • Please provide a [mcve] that demonstrates the issue, including some sample data and HTML/CSS. – geocodezip Aug 17 '16 at 02:30
  • Best I can tell your code works as posted ([fiddle](http://jsfiddle.net/ovfbvrt2/1/)). – geocodezip Aug 17 '16 at 02:56
  • I added a second location to the array and this replicates the issue, one marker shows the popup while the other does not – Josh Aug 18 '16 at 00:21
  • I could replicate your issue but I couldn't work out what is causing it. I got a suspicion that it could be something to do with the map bounds because when I tweaked the default `Lat` bounding to be `42.5` (`var customMap = new CustomMap('mapitems', 42.5, -72, 10);` and leaving the only marker to be at `42`, similarly the infoWindow would not open. – Samuel Toh Aug 18 '16 at 01:26
  • How did you add the second marker? Where is the [mcve]? – geocodezip Aug 18 '16 at 02:10
  • geocodezip I added it to the fiddle > http://jsfiddle.net/ovfbvrt2/2/ – Josh Aug 18 '16 at 03:30
  • @MistaJosh I think GoogleMaps must be confused by those prototype usages... I'm not sure to be honest. But you might want to try simplying things by following this example. http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example – Samuel Toh Aug 18 '16 at 09:24
  • @SamuelToh Fair enough, I love using prototypes when I can but I guess it may be a case of over engineering. Thanks for the help – Josh Aug 21 '16 at 21:06

0 Answers0