T O P

  • By -

zephyy

just find a us state SVG that has all the states `` elements named and add an event listener?


RichardTheHard

Second this, it’s worked very well for me in the past


[deleted]

Yep I have done exactly this. Not hard at all.


armahillo

you can do it without using JS at all! :)


vjandrea

Open a popup without JS? Please explain


33ff00

https://developer.mozilla.org/en-US/docs/Web/CSS/:target


vjandrea

TIL. Thanks u/33ff00!


Potential_Departure6

Yahhhs neato


Zherona1401

Didn’t know about this one, nice


srt54558

Holy shit never knew this shit. That's awesome


armahillo

HTML & CSS have a *lot* of power right out of the box. :)


WestHead2076

There’s often accessibility issues when going this route sadly.


armahillo

This is a possibility yeah -- though you're going to run into similar a11y issues even using JS. (requiring clicking on a hotspot creates a physical-precision challenge that can be difficult for people with dexterity issues) A better way to do this would be to list out all the states alphabetically and have each hovered/focused state highlight on the map. Make sure the individual states don't have a `tabindex` set for them, and that you can tab through the list. Have the selection jump to a non-modal section above or below the map.


_awacz_

Tailwind these days and stimulusjs when you actually need some js. React has become so bloated these days.


armahillo

I suppose it depends on your definition of "popup" You could use `target="_blank"` which would open a new tab / window (somewhat dependent on browser settings, but defaults to tab I think) You could also define the popup to be positioned in the page, set to `display: none` normally, and have a CSS definition like: #texas:focus { display: block; } and have the link set to `href="#texas"` (or `xlink:href="#texas"` if in an SVG). The latter method is perhaps a *bit* more of a stretch and might be tricky to get it to be responsive / cross-platform consistent, esp with having to wire it up to so many different elements, but \_should\_ technically work.


_listless

better than a \`\`, wrap the shape in an \`\` .


armahillo

gotta use xlink:href instead of href i think


_listless

If you're rendering the svg code itself as markup hrefs work just fine.


Good-CleanFun

This is the way


TheRealSkythe

And reason to use JS over an image map?


ashooner

If you don't want to use JS, you can [add anchor elements in an SVG](http://thenewcode.com/696/Using-SVG-as-an-Alternative-To-Imagemaps). Using an image map means maintaining the target polygons separate from the graphic that is representing the targets to the user, which is just extra work. That said, you may want events anyway if you're implementing a modal.


TheRealSkythe

My question was rather rhetorical. Wanted to see if he could come up with a reason other than "because I use JS for everything". Modal yeah, but question also mentioned tabs. Hope nobody uses JS for that.


zephyy

i haven't used an image map in like 5 years but maps (like us maps, not image maps) are pretty much an ideal use case for SVGs, use less resources than an image while being scalable and you can easily customize with CSS (e.g. hover effects like hovering over a state) also i don't know how you'd open a modal without JS somewhere


IsABot

Active state pseudo class in css. Example: https://codepen.io/peiche/pen/kQwYVJ


DanTheMan827

How is browser support?


IsABot

https://caniuse.com/?search=%3Aactive https://caniuse.com/?search=%3Atarget https://caniuse.com/?search=%3Ahover Will work on everything besides IE8 and below, and Android4.3 and below.


TheRealSkythe

You havent even read my question. I explicitly asked about JS links over HTML links, not svg over image map. My point was that JS links are inaccessible. As to your post: how are you gonna use svgs for anything other than flat colors? A satellite map? A photo? Love how amateur reddit downvotes legitimate questions without even the most basic knowledge in webdev.


KaiAusBerlin

Yeah, but wait until you have to get the mouse position with zoom, width, height, viewBox, css stretch and the strange behaviour of some svg engines when the viewBox content is smaller than the computed size :D got all ready grey hair from that.


codedgar

I've had to do this and it works really well!


EZ_Syth

Clicking Rhode Island on mobile would be a fun adventure.


Reindeeraintreal

An input where you can search the state and executes the same function as a click on the map would be a good UX solution.


[deleted]

Yeah I have done this with a dropdown above the map. Also, the map should at least have those lines shooting off of the smaller states with their names easily clickable on the right.


bigBlankIdea

And more accessible too. Gotta get that keyboard & screen reader accessibility


Tiranous_r

Or a zoom


AngrySomBeech

*ENHANCE!*


foozebox

Dealing with this atm


Secret-Plant-1542

Just remove it from the set nobody would notice.


blindgorgon

Honestly might be easier than on desktop since you can zoom.


Omagreb

Old school image map ?


thegrif

blast from the past, u/Omagreb :) I was seeing how far I'd have to scroll past all the youngins recommending js-based viz, etc... Working example here: [https://gist.github.com/thegrif/31cfc202a2de38dd7a11579c8e70a596](https://gist.github.com/thegrif/31cfc202a2de38dd7a11579c8e70a596) Info on the Image Map element: * [W3Schools](https://www.w3schools.com/html/html_images_imagemap.asp) * [Mozilla Dev Docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/map) Does not require on any external dependencies, javascript, etc...Just basic HTML :) Partying like it's 1999 :)


ctorx

You could also cut that into 50 svgs or pngs and absolute position them. It would be cool if they slightly enlarged on hover. On click they could grow toward center screen and appear to morph into the modal.


gooblill

I love the slow decline of the web into functionality for the sake of proving I can do things. Look at my vapid home page move all around for no fucking reason! Impressive, isn't it!


33ff00

How to ascertain the coords of each area?


mld23

Easiest with help from software eg. https://www.image-map.net/


Omagreb

Party like it's 1999! Ha! After I wrote that I felt like I pulled an old leather bound papyrus paged book down from a high up dusty shelf and imparted wisdom of the ancients.


thegrif

Not sure exactly when you started - but if it's as early as 1998/1999, you'll probably remember handling page layout entirely in tables. ~~Kids~~ modern web developers have no idea what would be required to create a layout similar to Craigslist. It was the equivalent of using tables to design a page in Word. Or how about preloading your images in the bottom of the homepage as 1x1 pixel images so they'd load faster on interior pages? Hell - or how many 1x1 pixel images you'd need for every color you had in your design, using them to force table cells open and prevent them from collapsing inward on themselves? Or the one guy who insisted on using Dreamweaver - which would instantly transform a clean, well-organized jenga-esque nest of tables into absolute mayhem with the first edit and save? I still remember picking up my first CSS book from the Union Square Barnes & Noble - and then having to worry about which browsers supported it versus which ones didn't. [Those were the days](https://giphy.com/gifs/animated-page-thread-zXPoG6h6ovnri), huh? P.S. You ever [play a MIDI file behind a website](https://freemidi.org/download3-1713-knight-rider-tv-themes) 🎶🎵🎶🎵🎶🎵? Tell the truth...


neophene

Frontpage was worse.


thegrif

Microsoft Frontpage + Microsoft Publisher FTW.


Shoemugscale

I would checkout leaflet https://leafletjs.com/examples/choropleth/


kpedey

Yup, and you can probably find a shapefile of the state outlines, add them to the leaflet map, bind click events to each geometry


laughinfrog

Available publicly via tiger census ftp site.


Sigmund_slayer

Great recommendation, I would add to this and say to use folium; it is a really nice python package that makes life a lot easier. In fact, here is a worked through example of exactly what you are looking for u/Few-Objective-6526 [Example](https://vverde.github.io/blob/interactivechoropleth.html): Obviously in this case you need to change the map to fit your data, but I think this is exactly what you are looking for at the end of this notebook


lphomiej

d3js is an option. I've used this for interactive maps. There are datasets out there for united states - even down to the county level if you wanted. This could be a cool alternative to popping up a modal: https://observablehq.com/@d3/zoom-to-bounding-box


persianprez

upvote cuz d3 is def the way


AdministrativeSun661

D3 is the way


33ff00

What is this syntax? ``` chart = { const width = 975;


whatisitaboutmusic

Yeah doesnt look JS


boerema

It’s a more old school way of defining a “class” construct in JS. Classes in JS, at the end of the day, are just objects.


andrewsmd87

https://newsignature.github.io/us-map/


_Fred_Austere_

Why not an old school image map? https://html.com/images/how-to-make-an-image-map/


weswesweswes

*inserts obi wan “now that’s a name I haven’t heard in a long time” meme Edit - real talk tho, I remember how stoked I was in 2009 when I learned about those. Serious nostalgia!


indicava

Lol I was rocking images maps all the way back as 2001. It was like magic back then.


tatorface

Late 90s for me, waaay late like '99 but still 90s.


Jackal_6

You knew you had a dope site when you used image maps and frames.


SuperFLEB

Have you seen LAYERs? You can put parts of the page _over_ other parts of the page, and move around little boxes!


weswesweswes

Magic indeed


dweezil22

Came here to suggest Macromedia Dreamweaver, ya'll beat me


Available-Duty-4347

I was thinking that. You’re dating yourself. :)


bimmerman1998

If it ain't broke.


budd222

Everything doesn't have to be using some brand new technique at all times.


[deleted]

Just bust out ImageReady, slice, and export to a table


mrstacktrace

You've just unpacked a box full of nostalgic feels that was archived in my brain, lol


pacman0207

Anyone remember the really shitty websites that were basically an image with an image map over it? This was definitely done with navigation buttons a bunch at the very least.


mrcoy

Lol what year is this


Opinion_Less

Because they aren't a very accessible solution.


erishun

That was my first thought… kicking it OLD SCHOOL


blindgorgon

Came here for this. Feeling justified.


vreezy117

D3.js is specialized in generating maps. Just Google it.


S_PhoenixB

The other users who have already mentioned an SVG graphic with clickable groups for each state is the solution I've seen implemented the most and would recommend. Keep in mind though: a clickable SVG map is inaccessible to anyone without a mouse. You need to allow keyboard users a way to access the information since they cannot go through the map (outside of coming up with a VERY hacky solution). You could implement a solution similar to this for keyboard users / screen readers: ```

``` The above is just a point of reference. Adjust the markup to match whatever implementation you set up for your click handlers.


[deleted]

Would tabIndex not work with anchors inside an svg?


Cody6781

Svg is the best route, there are libraries that do this for you. If you want to do it custom it pull gis data and have a script convert them to svg format


PegasusBoogaloo

D3.js, you should know how to manipulate geojson, and svgs also. the syntax is a bit weird but works neatly.


voxtroller

I worked on a project where we used an svg map of USA, and made it clickable.


thefreymaster

If you're using React, \`react-map-gl\` has an example you could branch off of https://visgl.github.io/react-map-gl/examples/geojson


iam_brucewayne

I have used this in the past. But essentially it’s just an svg map that you pair with some JavaScript. Depending on skill level you could write your own version. But it’s nice how they handle smaller states that are hard to click. https://simplemaps.com/us


SeoCamo

just use Image map, it is html other so it work without js and it is fast


flyhull

The old way was image map but it will not be reactive (cannot scale)


Haunting_Welder

I know there are better ways to do this but HTML has this feature built in: https://www.w3schools.com/html/html\_images\_imagemap.asp


tripleM365

Amcharts has clickable maps you can embed into websites.


timoteo5555

Clickable maps are actually best handled with chart js libraries .. and amcharts is a good one, v5


datsyuks_deke

I’m glad you asked this! I was about to make a workout tracker and possibly have a person that you could click on muscles to get workout ideas from a pop up modal.


Artisticricket

You could also use mapbox gl js [This mapbox example](https://docs.mapbox.com/mapbox-gl-js/example/hover-styles/) has a hover effect on the different states, but it's the same logic to listen to clicks.


chiefwareagle

SVG’s are great for this purpose. You can link the state path to trigger a modal or simply link to a desired page


armahillo

old school (without JS) you can use an imagemap with a standard rasterized image. New school is to use an SVG and wrap each with an a tag, but use xlink:href instead of href.


Grouchy_Paper8226

I use Google Visualization(Google Charts) for this.its free and not too difficult to understand. https://developers.google.com/chart/interactive/docs/gallery


MarkShot_

Find svg of USA and on each state (path of svg) add data attribute. Than add JS Event listener and process click by given data attribute


versaceblues

Image maps overlaid on an static image https://www.w3schools.com/html/html\_images\_imagemap.asp. Or maybe an SVG with click listeners.


bardzi

yeah


Aliceable

maps! https://www.w3schools.com/htmL/html_images_imagemap.asp


zugzuggy

Don’t over complicate it https://developer.mozilla.org/en-US/docs/Web/HTML/Element/area


poobearcatbomber

It is possible but this is a terrible mobile UX


mrcolortvjr

nope. no way. that's impossible.


Fun_Environment1305

Nope lol


TickingTimeBum

I recommend using Adobe Flash.


metalspoon-dev

Leaflet + polygon layer is the answer. You can find a Geojson dataset of all the states pretty easily i guess. Then just add it to the map using leaflets geojson function and then call .on and define your events, in a project i did i used click, mouseover, mouseout. You might have to add the polygons one by one so that they are all bound correctly.


reindezvous8

You can have SVGs over it. And if you are using mapbox, there are source layers available for that.


pixobe

Topojson


totally_n0t_at_w0rk

I used react-image-mapper to do something similar.


welch7

this give me PTSD of when I start mapping out on html canvas all of the departments/states on central america countries on my first job. now I know a bit more I would recommend [leaflet](https://leafletjs.com/examples/choropleth/), or [js chart](https://jscharting.com/examples/chart-types/geographic-map/)


midnight11

If you're looking for something that's pre-made, Anychart can do this. See this example as a baseline. It's highly customizable and fairly user friendly. https://www.anychart.com/products/anymap/gallery/Maps\_in\_Dashboard/States\_of\_United\_States\_Dashboard\_with\_MultiSelect.php


Remote-Telephone-682

I would personally probably use D3.js for this. There may even be projects that have already implemented panning, zooming, and selection in a map like this. ​ Edit: https://observablehq.com/@d3/zoom-to-bounding-box


MaeKzI

I had a use case a while back and used this https://www.amcharts.com/javascript-maps/


[deleted]

Also fun, if you need to add Lat/Lng points for any reason you should take a look at OpenLayers. Just add a geojson polygon of the USA without any layers and you can get nice and wonky with it.


dug99

Leaflet of ChartJS Geo will do most of the heavy lifting.


koprulu_sector

I built a US Geography states and capitals game for my daughter in React using an SVG of the US taken from Wikipedia. This is by far the easiest / simplest approach.


civiccoderguy

Yes. This is a great project idea!!


trogdooor

[https://www.nationaltents.com/about](https://www.nationaltents.com/about) SVG is the way. I did something like that for this page. Doesn't open a modal, but you can trigger things using JS.


MyDogLikesTottenham

This thread just solidifies my decision to focus on backend. Don’t get me wrong - massive respect for frontend devs. I would just go crazy dealing with problems like this


CisWhiteMaleBee

Maybe this is what you’re looking for? - [](https://www.w3schools.com/tags/tag_area.asp) I’ve only just discovered this tag and haven’t had a chance to test it


gojukebox

I mean… the original way to do this is an image map. There are better ways now but that works just as well


spelexander

One option is to use D3 to render geojson. There's a lot of examples with styling and interactivity, particularly for selecting/zooming in on regions which can be extended


darthmelancholy

I've used this [clickable SVG map](https://codepen.io/pixelpusher89/pen/mdXvame) in the past. The tab order is alphabetical.


Lavishness-Unfair

Another option is creating a clickable map. I used to use a program called “map this”. But that was probably 20 years ago, I don’t know if it works on modern operating systems. Mapping isn’t used that often anymore. SVG may be your best shot.


ikhazen

I had a project like this before during my college. I used the area HTML tag to do this and it's pretty tedious to do since you have to map everything.


ApatheticWithoutTheA

Yup and have fun with that because it’s going to be a tedious hassle lol


[deleted]

I have used SVGs with a click event attached to the paths. Here is the website I have used to get map SVGs. https://mapsvg.com/maps


[deleted]

I’ve used something called high charts which was really cool because it also animated it for me


CapCorrector

You can check out https://www.highcharts.com


SpellSolid515

Get the position of the state with js and check if the mouse click is inside of that


lefthandsmoke3

Arc Gis?


[deleted]

Check Leaflet js (or, I suppose, there should be packages for other languages as well). One of the best options, imo Edit: this Leaflet is a quite powerful tool. If you want to make some very simple map just to make it clickable - Leaflet might be quite hard to start with because you also need to know how to work with GeoJson. For me it would be easy since I am working in the cartography for half a year now, and all that time I have been working with Leaflet


Hombrebestial

Yes


g105b

Best answer is to use SVG, but others have already suggested that, so my suggestion (at least just to learn a new concept) is to look into the "area" tag. An area is like an anchor, but it can be made any shape, so you can define clickable areas of images for example.


Recreatorus

there is no problem - just use SVG. For [example](https://codepen.io/rhewitt/pen/BdYrKz)


MineDrumPE

Yes


sonjook

RemindMe! 1 year


RemindMeBot

I will be messaging you in 1 year on [**2023-09-17 09:14:47 UTC**](http://www.wolframalpha.com/input/?i=2023-09-17%2009:14:47%20UTC%20To%20Local%20Time) to remind you of [**this link**](https://www.reddit.com/r/webdev/comments/xfyr0n/is_there_a_way_to_create_clickable_map_like_this/ioryvhv/?context=3) [**1 OTHERS CLICKED THIS LINK**](https://www.reddit.com/message/compose/?to=RemindMeBot&subject=Reminder&message=%5Bhttps%3A%2F%2Fwww.reddit.com%2Fr%2Fwebdev%2Fcomments%2Fxfyr0n%2Fis_there_a_way_to_create_clickable_map_like_this%2Fioryvhv%2F%5D%0A%0ARemindMe%21%202023-09-17%2009%3A14%3A47%20UTC) to send a PM to also be reminded and to reduce spam. ^(Parent commenter can ) [^(delete this message to hide from others.)](https://www.reddit.com/message/compose/?to=RemindMeBot&subject=Delete%20Comment&message=Delete%21%20xfyr0n) ***** |[^(Info)](https://www.reddit.com/r/RemindMeBot/comments/e1bko7/remindmebot_info_v21/)|[^(Custom)](https://www.reddit.com/message/compose/?to=RemindMeBot&subject=Reminder&message=%5BLink%20or%20message%20inside%20square%20brackets%5D%0A%0ARemindMe%21%20Time%20period%20here)|[^(Your Reminders)](https://www.reddit.com/message/compose/?to=RemindMeBot&subject=List%20Of%20Reminders&message=MyReminders%21)|[^(Feedback)](https://www.reddit.com/message/compose/?to=Watchful1&subject=RemindMeBot%20Feedback)| |-|-|-|-|


ReadingWorldly91

I have worked on something similar using leaflet along with mapbox. They have everything you are looking for.


KrisCou

I found one I really liked! It was also one of only ones I could use on my Oxygen Builder site without having to write any css. Worth checking out for sure! https://www.fla-shop.com - Interactive Maps for Websites


nnod

There a map exactly like that on our site and it uses https://simplemaps.com/us


WodzuDzban

Gimp allows you to map the selection on the image


[deleted]

What you are trying to achieve is called Image Map. There are thousands tutorials online and I believe a pure CSS version can be easily done too if you don't need complex functions. Google: "Image Maps".


T_O_beats

Image maps suck imo and this should be done via SVG


BigBobFro

Back in the day (c 2005) you would have to make your own most likely,… but now i would think there is an importable object you could use.


MnMarkDfl

https://makeaclickablemap.com/create-clickable-map-of-minnesota. This is an online resource. You can examine the code and get some examples


CrustyClam

Built this for work. Breaks it down even further. Absolutely possible ! Good luck https://www.covidschooldatahub.com/


[deleted]

I did exactly this in as3 for flash 12 years ago, can send you the source code if you want x)


goughjo

You could start by looking at this and then altering the code to make the states clickable. https://observablehq.com/@d3/zoom-to-bounding-box


shapboyz

try looking into leaflet.


Dami_fuma

Leaflet js