Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8131931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:13:12+00:00 2026-06-06T09:13:12+00:00

I am using OpenLayers to create a map and plot locations. Each location has

  • 0

I am using OpenLayers to create a map and plot locations. Each location has a marker and a popup and are created using OpenLayers.Feature – at the moment, I’m definitely outside of my comfort zone here, so am cobbling example code together.

A marker is created as follows (I’ve chopped what I hope are obvious variable assignments for brevity):

function addMarker(ll, popupClass, popupContentHTML, closeBox, overflow, type)
{
    var feature = new OpenLayers.Feature(markerLayer, ll);  
    feature.closeBox = closeBox;
    feature.popupClass = popupClass;
    feature.data.icon = icon;
    feature.data.popupContentHTML = popupContentHTML;
    feature.data.overflow = (overflow) ? "auto" : "hidden";

    var marker = feature.createMarker();
    var markerClick = function (evt) {
        if (this.popup == null) {
            this.popup = this.createPopup(this.closeBox);
            map.addPopup(this.popup);
            this.popup.show();
        } else {
            this.popup.toggle();
    }
        currentPopup = this.popup;
        OpenLayers.Event.stop(evt);
    };

    marker.events.register("mousedown", feature, markerClick);
    markerLayer.addMarker(marker);
}

The map can contain many markers.

When a marker is clicked a popup toggles on and off. What I am trying to ado is make ALL popups relating to all markers on the map close when a new marker is clicked and a popup toggles on – that is, I only want one popup to display at a time.

It may be that my approach is all wrong, but would be grateful for pointers, even just ideas to try.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:13:13+00:00Added an answer on June 6, 2026 at 9:13 am

    IF you implement a solution whereas only one popup is active at a time (i.e. every time a popup is unselected it disappears), you will NEVER have more than one popup at a time.

    read this STACKOVERFLOW answer which i wrote for exactly this problem. you have all the necessary pseudocode there (with lengthy explanations about everything).

    if you dont need the explanations, this shows the solution:

    var urlKML = 'parseKMLTrack07d.php';         
    var layerKML = new OpenLayers.Layer.Vector("KML1", {
                strategies: [new OpenLayers.Strategy.Fixed()],
                protocol: new OpenLayers.Protocol.HTTP({
                    url: urlKML,
                    format: new OpenLayers.Format.KML({
                        extractStyles: true, 
                        extractAttributes: true,
                        maxDepth: 2
                    })
                })
            });
    
    var layerOSM = new OpenLayers.Layer.OSM();
    var map = new OpenLayers.Map({
        div: "map",
        layers: [
            layerOSM,
            layerKML 
        ]
    });
    
    var selectStop = new OpenLayers.Control.SelectFeature(layerKML,{onSelect: onFeatureSelect, onUnselect: onFeatureUnselect});
    layerKML.events.on({
                "featureselected": onFeatureSelect,
                "featureunselected": onFeatureUnselect
            });
    map.addControl(selectStop);
    selectStop.activate();
    
    function onFeatureSelect(event) {
        var feature = event.feature;
        var content = feature.attributes.name + '<br/>'+feature.attributes.description;
        popup = new OpenLayers.Popup.FramedCloud("chicken", 
                                 feature.geometry.getBounds().getCenterLonLat(),
                                 new OpenLayers.Size(100,100),
                                 content,
                                 null, true, onFeatureUnselect);
        feature.popup = popup;
        map.addPopup(popup);
        // GLOBAL variable, in case popup is destroyed by clicking CLOSE box
        lastfeature = feature;
    }
    function onFeatureUnselect(event) {
        var feature = lastfeature;  
        if(feature.popup) {
            map.removePopup(feature.popup);
            feature.popup.destroy();
            delete feature.popup;
        }
    }
    

    now, if you REALLY want to destroy all popups, regardless (which i very much discourage):

    function popupClear() {
        //alert('number of popups '+map.popups.length);
        while( map.popups.length ) {
             map.removePopup(map.popups[0]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very simple question, how can I create a draggable marker in an OpenLayers map,
I'm trying to plot MGRS lines over a map in an overlay using OpenLayers
I'm using OpenLayers map in my project,I'm displaying list of items on map.Whenever user
I am using Openlayers to my application. i have succeeded in showing marker when
Is it possible to draw line by scratching on the map using OpenLayers? I
I'm using Patrick Wied's OpenLayers Heatmap layer, but only for locations in the UK.
I have created a small test page using Sencha Touch , OpenLayers and I
I currently use a google map and create tiles for it by using a
I'm using OpenLayers to display a map in a web page. I am using
I'm using OpenLayers to display a map on a standard HTML page, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.