My situation:

  • I have administrative areas marked with polygons
  • Many areas have many, many, many islands
  • I need main area and all it's islands to change color when any of the islands or main is hovered
  • I currently need to make if statement for main area + each island in mouseover and mouseout event which would be a lot of if statements in total

Is there a solution or workaround for this?

Could I group them somehow and use that group or something to easily change options or do something else for the whole group?

  • Are you working with multipolygons, or just polygons linked by a common attribute? – alphabetasoup Jan 18 '16 at 10:56
  • @RichardLaw I have numberous different/separate complex polygons, all are made as single polygons. How could I link them? It would be easy to change them all in that case. – N00b Jan 18 '16 at 13:35
  • Yes, you could union them. – alphabetasoup Jan 18 '16 at 19:06

Ideally, you should be using GeoJSON rather than your own arbitrary JSON. Then, take your Polygon geometries and create Multipolygon geometries—perhaps using the union capability of turf.js. This way, simple polygons that are in fact disconnected parts of the same multipolygon are linked. You can also do this upfront, but it's not clear how you are getting your data.

  • Thank you for answering. Im pretty sure that GeoJSON is not an option because I need to send other data too with same ajax requests. But it shouldn't be a problem because quick search online showed that my structure of polygon data is very similar to GeoJSON structure (I might miss something important about GeoJSON, I've never worked with it). I solved my problem by pushing all main lands to their own arrays in overall array and I also pushed all separate lands to same array where their main land is. Now I can use for loop to affect each area with all it's parts at once. – N00b Jan 19 '16 at 1:06
  • GeoJSON is still JSON, it just has a well-known structure and the concept of a multipolygon is already defined for you, and client applications will also be able to interpret it. It's hard to say anything more without seeing the structure of your JSON, but using a GeoJSON multipolygon would be the standard way to address this. – alphabetasoup Jan 19 '16 at 1:12

Can you modify the dataset so that each island has an admin area attribute. Then you can just get the admin area of the polygon you're hovering over; and then change colour to polygons if their admin area attribute matches (within your mouse over event)

  • How can I give dataset to polygon? I can't even find polygons from DOM. – N00b Jan 18 '16 at 19:45
  • Not sure I understand the question totally but; what i'm trying to say is that the dataset you are using, can you modify it in a GIS so that the administration area is attributed for each polygon/feature/island (whatever you want to call it). Then put use this updated dataset in your map. Is your data JSON, KML, from a web service? – Liam G Jan 18 '16 at 22:16
  • Data is in JSON but I parse it to object that contains coordinates, area name and other values. – N00b Jan 18 '16 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.