omeka / plugin-Geolocation

Allows administrative users to save geolocation data for an individual item. The plugin generates a Google map containing items with geolocation data and a web page for displaying the map.
http://omeka.org/codex/Plugins/Geolocation
12 stars 20 forks source link

bindHtml in map.js #42

Closed adehner closed 8 years ago

adehner commented 8 years ago

This may not belong in issues, but I have a question about bindHtml in map.js

I incorporated mapclusterer and overlapping marker spiderfier into the Geolocation plugin. All is working but the infoWindows. Before I incorporated spiderfier, infoWindows opened as expected, with the correct title, image and description.

After adding spiderfier, the infoWindow doesn't know which title/image/description to open from bindHtml. The same infoWindow content opens for every marker. I think it's the infoWindow for the last marker in bindHtml. Spiderfier asks that I replace the google event listener with a specific spiderfier listener. And I believe this is where my problem lies, but I'm not sure how to correct it. I'm pasting my code for addMarker below, in hopes that you can tell me what I'm doing wrong.

addMarker: function (lat, lng, options, bindHtml)
    {     
        if (!options) {
            options = {};
        }
        options.position = new google.maps.LatLng(lat, lng);
        options.map = this.map;
        var marker = new google.maps.Marker(options);

        if (bindHtml) {
            var infoWindow = new google.maps.InfoWindow({
                content: bindHtml
            });

            var that = this;

        this.oms.addListener('click', function(marker, event) {
        // Prevent multiple windows from being open at once.
        if (that.lastWindow) {
                    that.lastWindow.close();
                }
                that.lastWindow = infoWindow;
        infoWindow.open(this.map, marker);
        });
        }
        this.oms.addMarker(marker);
        this.markers.push(marker);
        this.markerBounds.extend(options.position);
        return marker;
    },
zerocrates commented 8 years ago

The problem is that the OMS addListener isn't scoped to an individual marker: it runs that listener whenever any marker is clicked, and just passes the event the marker that actually got clicked that time. What's probably happening is that every one of the handlers is being called one after the other, briefly opening the window for each marker, until the last one, which is the only one that stays open.

Basically, the OMS addListener is designed to be called just once, not one time for each marker like our original code that uses the Google Maps addListener is. It also basically requires you to put your content or InfoWindow into the marker data itself, since that's all the context you get passed.

You could try the following things:

adehner commented 8 years ago

Thank you! Your first suggestion did the trick.