ojamas / nyc-street-parking

Interactive map for viewing NYC street parking regulations
2 stars 0 forks source link

selecting neighborhood leaves neighborhood layer #5

Closed ojamas closed 7 years ago

ojamas commented 7 years ago

Currently when selecting a feature, the layer that the feature belongs to and the feature itself remain on the map. There is no easy way to remove just that feature - instead the whole layer has to be removed.

I created a workaround for selecting boroughs and showing only the neighborhoods of the selected boro while still being able to select a different boro by creating a new query to return only the selected boro's neighborhoods... This is not ideal as it involves another query and API call.

This seems to be a potential solution but I have not determined how or if it will work for my implementation.

GOAL: It would be nice to be able to select a boro or neighborhood and have only that feature removed from the map to reveal the layer "below" it. For example, selecting Manhattan would display a map containing the Manhattan neighborhoods and the other four boros. Selecting Lower East Side within Manhattan would display the streets of the Lower East Side as well as the surrounding neighborhoods as seen before selecting Lower East Side.

ojamas commented 7 years ago

FIX: Using two new objects and Leaflet Feature Groups for both boroughs and neighborhoods, I have made it so that each feature can be hidden/shown independent of the "layer category" (such as boroughs).

Objects: boroArr & nbhdArr These objects are constructed during the onEachFeature functions for boroughs and neighborhoods. They map the names of the boroughs or neighborhoods as properties to the Layer object itself. NOTE: while they are technically objects in Javascript, they are created as arrays. This method allows for dynamic property definitions, needed in this implementation.

Creation: boroArr[feature.properties.boro_name] = layer;

Example: boroArr = {"Brooklyn" = Layer Object, "Bronx" = Layer Object ... etc}

These layers can then be added dynamically to the Feature Groups (boroFG and nbhdFG). This allows a single borough to be removed upon selection and then replaced when selecting another borough. Neighborhoods function in much the same way.

FUTURE: I'm not positive this approach will prove perfect for the future. There is also the question of performance and memory, I would imagine, since each array/object must hold all of the features at the same time. There are a finite amount of boroughs and neighborhoods, so at least this issue shouldn't scale.