iitc-project / ingress-intel-total-conversion

ingress.com/intel total conversion user script with some new features. Should allow easier extension of the intel map.
http://iitc.jonatkins.com/
ISC License
989 stars 552 forks source link

addLayerGroup - problem with geojson layer #349

Closed StilgarBF closed 11 years ago

StilgarBF commented 11 years ago

I'm not sure where to find help with a developmen-related question, so I try my luck here.

I'm working on a plugin to show portals that have been suggested on the map. The data is fetched with jsonp from my site and than processed as geojson. It works so far, but I want to add a new option to the layer-group palette. The option shows up, but does not toggle the layer.

The layer has been created with

window.plugin.portalSuggestions.markerLayerGroup = L.geoJson(data,
{ ...
}).addTo(map);

When trying to remove the layer from the console it works with

map.removeLayer(window.plugin.portalSuggestions.markerLayerGroup)

What am I doing wrong here?

The plugin can be reviewed in my fork: https://github.com/StilgarBF/ingress-intel-total-conversion/blob/gh-pages/plugins/portal-suggestions.user.js

jonatkins commented 11 years ago

You need to add the layer to the layer chooser as well. That could be done directly, but there's a utility function that will do this for you

window.addLayerGroup('LAYER_NAME', LAYER_GROUP, true);

the 3rd parameter is true/false for if you want the layer to default to on or off

Also, this will handle saving the layer state, so next time you load IITC it remains the same.

StilgarBF commented 11 years ago

Had done this in the first place, as said before "The option shows up, but does not toggle the layer."

window.addLayerGroup('Portal suggestions', window.plugin.portalSuggestions.markerLayerGroup, true);

I'm a bit confused. Because it seems as if I have done the same things that have been done in other plugins. The only difference I can see is that my layer is created by L.geoJson.

Maybe I have to set up another dummy plugin to check that.

If you want to give the plugin a try . it is linked above. Focus on "Nürnberg" / Nurenberg Germany and trigger the load of the suggestions with the link in the toolbox. - seems to work fine, but I can not hide the layer.

jonatkins commented 11 years ago

OK, had a closer look.

  1. you create an empty layer group (line 27)
  2. you add this to the layer chooser (line 129)
  3. you replace this empty layer group variable with a new layer loaded with geoJson (line 78)

however, step 3 doesn't replace the layer in the chooser with the new geoJson layer - the layer chooser still holds onto the blank layer.

What you need to do instead is, rather than replacing your blank LayerGroup with the getJson response, you need to ADD your layer returned to the existing layer group.

i.e. instead of

window.plugin.portalSuggestions.markerLayerGroup = L.geoJson(data,
  ........
}).addTo(map);

do

window.plugin.portalSuggestions.markerLayerGroup.addLayer ( L.geoJson(data,
  ........
}));
StilgarBF commented 11 years ago

Thanks - that sounds like the problem.

edit: worked perfectly. thanks again.

In context of that plugin: Do you want new plugins as pullrequests to this repo? There are not all available plugins in the plugin-folder.

jonatkins commented 11 years ago

I do accent new plugins, with some restrictions:

pull requests are preferred, but I'll take it in other forms too if it's easier.