GithubHelp home page GithubHelp logo

map layer checkbox event about fgpv-vpgf HOT 7 CLOSED

prosperva avatar prosperva commented on June 16, 2024
map layer checkbox event

from fgpv-vpgf.

Comments (7)

james-rae avatar james-rae commented on June 16, 2024

I don't think there is a "global" event that raises for any layer.

You would need to watch observables on each individual layer. Also, this will trigger if anything changes the visibility; not just mouse clicks on legend checkbox toggles.

e.g.

var myLayer = RAMP.mapById('rv-app-0').layers.allLayers[0];
myLayer.visibilityChanged.subscribe(e => {
  console.log('I am visible? : ' + e); // e is boolean
});

You can use the MapAPI.layers.layerAdded observable if you need to watch for new layers coming in (to add listeners to their observable).

from fgpv-vpgf.

prosperva avatar prosperva commented on June 16, 2024

Thanks. Is there a way to check when RAMP has been added to the DOM?

from fgpv-vpgf.

james-rae avatar james-rae commented on June 16, 2024

RAMP.mapAdded.subscribe(map => { do stuff }) but to be honest I rarely use this and am not sure how the timing works. If a map is generated in the page lifetime it should show up on this observable. For maps that are hard-defined in the DOM (e.g. a div with is="rv-map"), I'm not sure...it might exist before that observable. Might need to test it on your end, but I would hope it fires lol. If not you can check the .mapInstances array for pre-existing maps before listening.

from fgpv-vpgf.

prosperva avatar prosperva commented on June 16, 2024

I came up with this solution but it is firing before RAMP has been added to the DOM

function remove(arr, value) {
    return arr.filter(function (ele) {
      return ele != value;
    });
  }

  $(".rv-list-item-controls").click(function (checkbox) {
    let selectedLayers = [];
    RAMP.mapById("rv-app-0").layers.allLayers.forEach(function (e) {
      if (e.visibility) {
        selectedLayers.push(e.id);
      } else {
        remove(selectedLayers, e.id);
      }
    });
    localStorage.setItem("selectedLayers", selectedLayers);
  });

  if (localStorage.getItem("selectedLayers") != null) {
    let loadLayers = localStorage.getItem("selectedLayers").split(",");
    if (typeof loadLayers != "undefined" && loadLayers.length > 0) {
      loadLayers.forEach(function (e) {
        RAMP.mapById("rv-app-0").layers.getLayersById(e)[0].visibility = true;
      });
    }
  }

EDIT: Culprit line RAMP.mapById("rv-app-0").layers.getLayersById(e)[0].visibility = true;

from fgpv-vpgf.

james-rae avatar james-rae commented on June 16, 2024

I have very little context in what you're trying to do, but am guessing the bottom part

RAMP.mapById("rv-app-0").layers.getLayersById(e)[0].visibility = true;

runs too early. Is this script running before the ramp script? If so, put it after. If it's already after, you may need to do some waiting until your instance appears. I.e. waiting for the instance, and possibly waiting for layers. I don't know how your site starts up or the requirements so you'll need to do what works for you. I love setTimeouts for glorious busywait hacks.

Alternately, if your setup can work with it, you could consider trying to leverage the bookmarking module.

See this sample page

  • This line, notice data-rv-wait="true" is set. This does a block to allow any potential bookmark to be consumed before finishing the load.
  • This line shows how to set a bookmark (you would have stored it in your local storage). Notice it uses the "legacy API" RV, you'll need the legacy script if you're not already using it. Also note if you had nothing in your local storage, you'd need to call this with empty parameter to unblock the instance.
  • This line shows how to grab a bookmark of the current state. So whenever you want to save your state, you'd do this and put in local storage instead of the document call this sample does.

The bookmark will track visibility, opacity, map extent, etc. So might not be what you want.
The sample page here isn't great since has no layers but you could add a dummy layer for local testing if you wanted (corresponding config is here)

Might be more trouble than it's worth, up to you.

from fgpv-vpgf.

james-rae avatar james-rae commented on June 16, 2024

Also it only works with layers that are in your config file. If you have random layers getting dropped in via API calls, don't bother with the bookmark module

from fgpv-vpgf.

prosperva avatar prosperva commented on June 16, 2024

I ended up manipulating the config file using some custom code and it worked. The project I am on uses React so it adds some complexity. Thanks for your guidance

from fgpv-vpgf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.