Codename One - Flags for all countries - codenameone
I have a code very similar to the Uber clone app to show a country picker form. I understand that the flags are taken from flagResource = Resources.open("/flags.res");, from the Codename One SMS Library
The problem is that a lot of flags are missing. A user may be sorry if the flag of his/her country is not there.
How can I insert all the flags?
I wish I knew. I looked a lot for a resource that contains all of the flags in the world and was very disappointed that most were lacking in various ways.
After I did all that I thought about using the unicode emojii flags which would be a huge boon as it would also remove the need for images but that wouldn't work in the simulator properly as it doesn't render emojii. It might be interesting to try and enhance the code with support for emojii on devices where it's supported but you will need to actually do the legwork of going through almost 200 countries and finding their flag unicode value... There are some lists but adapting them is a pain.
I found a solution to show all the flags. I'm going to share my code and exactly how I proceeded...
STEP 1 - HOW MANY COUNTRY CODES?
Complete List of Country & Dialing Codes (248 country codes)
- https://www.worldatlas.com/aatlas/ctycodes.htm
I compared the previous list with these:
https://en.wikipedia.org/wiki/List_of_country_calling_codes (280
country code, but missing of iso codes)
https://countrycode.org/ (240 country codes)
http://www.countryareacode.net/ (225 country codes)
https://www.studyabroad.com/resources/international-calling-codes
(234 country codes)
https://www.howtocallabroad.com/codes.html (255 country codes,
but some of them are repeated)
The first list with 248 country codes is probabily the one to be used. Note that 248 country codes were used also by Whatsapp (in the 2016), it's an hint of the right direction:
https://www.quora.com/Whatsapp-has-248-flags-in-its-symbols-keyboard-in-its-latest-update-But-there-are-nearly-190-200-countries-on-earth-What-is-it-that-I-am-missing
However the current version of Whatsapp has 258 flags (I counted them extracting them from its apk). However, even if I suppose that the flags cannot fall within anyone's intellectual property, I think that it's safer to use flags from an open source repository, like the following one.
I'm going to use the 248 country codes list because it's the more suitable for our purposes (in my opinion, after several checkings).
STEP 2 - DOWNLOAD THE FLAGS
The 248 flags (in SVG format) can be downloaded from:
https://github.com/joielechong/iso-country-flags-svg-collection
The interesting folder for our purposes is this one:
iso-country-flags-svg-collection-master/svg/country-4x3
It contains more flags than the 248 available country codes (they are 262 in total), but it's not a problem (the opposite should be a problem, of course...).
I converted all of them to PNGs files with the size of 150x112 pixels with the following command (on Linux with Inkscape installed):
for i in *.svg; do inkscape $i --export-width=150 --export-height=112 --export-png=`echo $i | sed -e 's/svg$/png/'`; done
You can download these generated PNGs from: http://jmp.sh/vXaA7eC
STEP 3 - CREATE NEW ARRAYS FOR THE APP
In the Uber clone, we have the following arrays, each of them contains 243 strings:
COUNTRY_NAMES (Afghanistan to Zimbabwe, note that Åland Islands is at
the end of the list after Zimbabwe, I suppose that it's not correct
in alphabetic order... however the country code of Åland Islands is
the same of Finland)
COUNTRY_CODES (all numeric codes, without the "+")
COUNTRY_FLAGS (all the files names of the PNGs, with "null" for some countries)
COUNTRY_ISO2 (AL, DZ, AS, etc.)
COUNTRY_ISO3 (AFG, ALB, etc.)
I replaced the import of these array from com.codename1.sms.activation.ActivationForm.* with the following ones declared in my package (all of them contains 248 strings):
public static final String[] COUNTRY_NAMES = {"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bonaire", "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Democratic Republic of the Congo", "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Curacao", "Cyprus", "Czech Republic", "Cote d'Ivoire", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard Island and McDonald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran, Islamic Republic of", "Iraq", "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macao", "Macedonia, the Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Palestine, State of", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Romania", "Russian Federation", "Rwanda", "Reunion", "Saint Barthelemy", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Martin (French part)", "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Sint Maarten (Dutch part)", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Sudan", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "United Republic of Tanzania", "Thailand", "Timor-Leste", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Viet Nam", "Virgin Islands (British)", "US Virgin Islands", "Wallis and Futuna", "Western Sahara", "Yemen", "Zambia", "Zimbabwe"};
// note: from the original list https://www.worldatlas.com/aatlas/ctycodes.htm, I modified the code of "Dominican Republic", I removed the "-" sign from all codes and I corrected some typos in the country names
public static final String[] COUNTRY_CODES = {"93", "355", "213", "1684", "376", "244", "1264", "672", "1268", "54", "374", "297", "61", "43", "994", "1242", "973", "880", "1246", "375", "32", "501", "229", "1441", "975", "591", "599", "387", "267", "47", "55", "246", "673", "359", "226", "257", "855", "237", "1", "238", "1345", "236", "235", "56", "86", "61", "61", "57", "269", "242", "243", "682", "506", "385", "53", "599", "357", "420", "225", "45", "253", "1767", "18", "593", "20", "503", "240", "291", "372", "251", "500", "298", "679", "358", "33", "594", "689", "262", "241", "220", "995", "49", "233", "350", "30", "299", "1473", "590", "1671", "502", "44", "224", "245", "592", "509", "672", "379", "504", "852", "36", "354", "91", "62", "98", "964", "353", "44", "972", "39", "1876", "81", "44", "962", "7", "254", "686", "850", "82", "965", "996", "856", "371", "961", "266", "231", "218", "423", "370", "352", "853", "389", "261", "265", "60", "960", "223", "356", "692", "596", "222", "230", "262", "52", "691", "373", "377", "976", "382", "1664", "212", "258", "95", "264", "674", "977", "31", "687", "64", "505", "227", "234", "683", "672", "1670", "47", "968", "92", "680", "970", "507", "675", "595", "51", "63", "870", "48", "351", "1", "974", "40", "7", "250", "262", "590", "290", "1869", "1758", "590", "508", "1784", "685", "378", "239", "966", "221", "381", "248", "232", "65", "1721", "421", "386", "677", "252", "27", "500", "211", "34", "94", "249", "597", "47", "268", "46", "41", "963", "886", "992", "255", "66", "670", "228", "690", "676", "1868", "216", "90", "993", "1649", "688", "256", "380", "971", "44", "1", "1", "598", "998", "678", "58", "84", "1284", "1340", "681", "212", "967", "260", "263"};
// here I list the file names in the same order of the previous country names and country codes; some unused flags are not listed, because we have more flags than country codes...
public static final String[] COUNTRY_FLAGS = {"af.png", "al.png", "dz.png", "as.png", "ad.png", "ao.png", "ai.png", "aq.png", "ag.png", "ar.png", "am.png", "aw.png", "au.png", "at.png", "az.png", "bs.png", "bh.png", "bd.png", "bb.png", "by.png", "be.png", "bz.png", "bj.png", "bm.png", "bt.png", "bo.png", "bq.png", "ba.png", "bw.png", "bv.png", "br.png", "io.png", "bn.png", "bg.png", "bf.png", "bi.png", "kh.png", "cm.png", "ca.png", "cv.png", "ky.png", "cf.png", "td.png", "cl.png", "cn.png", "cx.png", "cc.png", "co.png", "km.png", "cg.png", "cd.png", "ck.png", "cr.png", "hr.png", "cu.png", "cw.png", "cy.png", "cz.png", "ci.png", "dk.png", "dj.png", "dm.png", "do.png", "ec.png", "eg.png", "sv.png", "gq.png", "er.png", "ee.png", "et.png", "fk.png", "fo.png", "fj.png", "fi.png", "fr.png", "gf.png", "pf.png", "tf.png", "ga.png", "gm.png", "ge.png", "de.png", "gh.png", "gi.png", "gr.png", "gl.png", "gd.png", "gp.png", "gu.png", "gt.png", "gg.png", "gn.png", "gw.png", "gy.png", "ht.png", "hm.png", "va.png", "hn.png", "hk.png", "hu.png", "is.png", "in.png", "id.png", "ir.png", "iq.png", "ie.png", "im.png", "il.png", "it.png", "jm.png", "jp.png", "je.png", "jo.png", "kz.png", "ke.png", "ki.png", "kp.png", "kr.png", "kw.png", "kg.png", "la.png", "lv.png", "lb.png", "ls.png", "lr.png", "ly.png", "li.png", "lt.png", "lu.png", "mo.png", "mk.png", "mg.png", "mw.png", "my.png", "mv.png", "ml.png", "mt.png", "mh.png", "mq.png", "mr.png", "mu.png", "yt.png", "mx.png", "fm.png", "md.png", "mc.png", "mn.png", "me.png", "ms.png", "ma.png", "mz.png", "mm.png", "na.png", "nr.png", "np.png", "nl.png", "nc.png", "nz.png", "ni.png", "ne.png", "ng.png", "nu.png", "nf.png", "mp.png", "no.png", "om.png", "pk.png", "pw.png", "ps.png", "pa.png", "pg.png", "py.png", "pe.png", "ph.png", "pn.png", "pl.png", "pt.png", "pr.png", "qa.png", "ro.png", "ru.png", "rw.png", "re.png", "bl.png", "sh.png", "kn.png", "lc.png", "mf.png", "pm.png", "vc.png", "ws.png", "sm.png", "st.png", "sa.png", "sn.png", "rs.png", "sc.png", "sl.png", "sg.png", "sx.png", "sk.png", "si.png", "sb.png", "so.png", "za.png", "gs.png", "ss.png", "es.png", "lk.png", "sd.png", "sr.png", "sj.png", "sz.png", "se.png", "ch.png", "sy.png", "tw.png", "tj.png", "tz.png", "th.png", "tl.png", "tg.png", "tk.png", "to.png", "tt.png", "tn.png", "tr.png", "tm.png", "tc.png", "tv.png", "ug.png", "ua.png", "ae.png", "gb.png", "us.png", "um.png", "uy.png", "uz.png", "vu.png", "ve.png", "vn.png", "vg.png", "vi.png", "wf.png", "eh.png", "ye.png", "zm.png", "zw.png"};
// note that the file names should be lowercase, instead the country iso codes should be uppercase
public static final String[] COUNTRY_ISO2 = {"AF", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CG", "CD", "CK", "CR", "HR", "CU", "CW", "CY", "CZ", "CI", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM", "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", "KP", "KR", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RO", "RU", "RW", "RE", "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", "SC", "SL", "SG", "SX", "SK", "SI", "SB", "SO", "ZA", "GS", "SS", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW"};
public static final String[] COUNTRY_ISO3 = {"AFG", "ALB", "DZA", "ASM", "AND", "AGO", "AIA", "ATA", "ATG", "ARG", "ARM", "ABW", "AUS", "AUT", "AZE", "BHS", "BHR", "BGD", "BRB", "BLR", "BEL", "BLZ", "BEN", "BMU", "BTN", "BOL", "BES", "BIH", "BWA", "BVT", "BRA", "IOT", "BRN", "BGR", "BFA", "BDI", "KHM", "CMR", "CAN", "CPV", "CYM", "CAF", "TCD", "CHL", "CHN", "CXR", "CCK", "COL", "COM", "COG", "COD", "COK", "CRI", "HRV", "CUB", "CUW", "CYP", "CZE", "CIV", "DNK", "DJI", "DMA", "DOM", "ECU", "EGY", "SLV", "GNQ", "ERI", "EST", "ETH", "FLK", "FRO", "FJI", "FIN", "FRA", "GUF", "PYF", "ATF", "GAB", "GMB", "GEO", "DEU", "GHA", "GIB", "GRC", "GRL", "GRD", "GLP", "GUM", "GTM", "GGY", "GIN", "GNB", "GUY", "HTI", "HMD", "VAT", "HND", "HKG", "HUN", "ISL", "IND", "IDN", "IRN", "IRQ", "IRL", "IMN", "ISR", "ITA", "JAM", "JPN", "JEY", "JOR", "KAZ", "KEN", "KIR", "PRK", "KOR", "KWT", "KGZ", "LAO", "LVA", "LBN", "LSO", "LBR", "LBY", "LIE", "LTU", "LUX", "MAC", "MKD", "MDG", "MWI", "MYS", "MDV", "MLI", "MLT", "MHL", "MTQ", "MRT", "MUS", "MYT", "MEX", "FSM", "MDA", "MCO", "MNG", "MNE", "MSR", "MAR", "MOZ", "MMR", "NAM", "NRU", "NPL", "NLD", "NCL", "NZL", "NIC", "NER", "NGA", "NIU", "NFK", "MNP", "NOR", "OMN", "PAK", "PLW", "PSE", "PAN", "PNG", "PRY", "PER", "PHL", "PCN", "POL", "PRT", "PRI", "QAT", "ROU", "RUS", "RWA", "REU", "BLM", "SHN", "KNA", "LCA", "MAF", "SPM", "VCT", "WSM", "SMR", "STP", "SAU", "SEN", "SRB", "SYC", "SLE", "SGP", "SXM", "SVK", "SVN", "SLB", "SOM", "ZAF", "SGS", "SSD", "ESP", "LKA", "SDN", "SUR", "SJM", "SWZ", "SWE", "CHE", "SYR", "TWN", "TJK", "TZA", "THA", "TLS", "TGO", "TKL", "TON", "TTO", "TUN", "TUR", "TKM", "TCA", "TUV", "UGA", "UKR", "ARE", "GBR", "USA", "UMI", "URY", "UZB", "VUT", "VEN", "VNM", "VGB", "VIR", "WLF", "ESH", "YEM", "ZMB", "ZWE"};
STEP 4 - Put all together
From CountryCodePicker.java and CountryPickerForm.java (of the Uber Clone) remove the import of the arrays from com.codename1.sms.activation.ActivationForm and insert the previous arrays in your package and import them from it.
In the Codename One Designer, create a new file 248flags.res and import all the PNG flags generated at the Step 2 (http://jmp.sh/vXaA7eC) with "Images" -> "Quick Add Multiimages", selecting "4K" as source resolution. Wait for a while, it can be a long task...
In the CountryCodePicker.java, change:
flagResource = Resources.open("/flags.res");
to:
flagResource = Resources.open("/248flags.res");
STEP 5 - Result
Good news: now there are all the flags!!! :)
About the build size, the original flag.res of SMSActivation.cn1lib has a size of 411 kbyte (it's located in lib/impl/cls/flags.res), while my 248flags.res has a size of 2596 kbyte... but the original flag.res uses normal images of about 100x50 pixels, instead I used multiimages that better adapt on different densities.
Final check: it's possibile to manually check all country names and codes on Wikipedia and on other resources... if somebody wants to do the check, thanks :)
Related
How do you store a JSON Object like this in a React Hook UseState?
{ 'New-York': [ "New York", "Buffalo", "Rochester", "Yonkers", "Syracuse", "Albany", "New Rochelle", "Mount Vernon", "Schenectady", "Utica", "White Plains", "Hempstead", "Troy", "Niagara Falls", "Binghamton", "Freeport", "Valley Stream" ],"California": [ "Los Angeles", "San Diego", "San Jose", "San Francisco", "Fresno", "Sacramento", "Long Beach", "Oakland", "Bakersfield", "Anaheim", "Santa Ana", "Riverside", "Stockton", "Chula Vista", "Irvine", "Fremont", "San Bernardino", "Modesto", "Fontana", "Oxnard", "Moreno Valley", "Huntington Beach", "Glendale", "Santa Clarita", "Garden Grove", "Oceanside", "Rancho Cucamonga", "Santa Rosa", "Ontario", "Lancaster", "Elk Grove", "Corona", "Palmdale", "Salinas", "Pomona", "Hayward", "Escondido", "Torrance", "Sunnyvale", "Orange", "Fullerton", "Pasadena", "Thousand Oaks", "Visalia", "Simi Valley", "Concord", "Roseville", "Victorville", "Santa Clara", "Vallejo", "Berkeley", "El Monte", "Downey", "Costa Mesa", "Inglewood", "Carlsbad", "San Buenaventura (Ventura)", "Fairfield", "West Covina", "Murrieta", "Richmond", "Norwalk", "Antioch", "Temecula", "Burbank", "Daly City", "Rialto", "Santa Maria", "El Cajon", "San Mateo", "Clovis", "Compton", "Jurupa Valley", "Vista", "South Gate", "Mission Viejo", "Vacaville", "Carson", "Hesperia", "Santa Monica", "Westminster", "Redding", "Santa Barbara", "Chico", "Newport Beach", "San Leandro", "San Marcos", "Whittier", "Hawthorne", "Citrus Heights", "Tracy", "Alhambra", "Livermore", "Buena Park", "Menifee", "Hemet", "Lakewood", "Merced", "Chino", "Indio", "Redwood City", "Lake Forest", "Napa", "Tustin", "Bellflower", "Mountain View", "Chino Hills", "Baldwin Park", "Alameda", "Upland", "San Ramon", "Folsom", "Pleasanton", "Union City", "Perris", "Manteca", "Lynwood", "Apple Valley", "Redlands", "Turlock", "Milpitas", "Redondo Beach", "Rancho Cordova", "Yorba Linda", "Palo Alto", "Davis", "Camarillo", "Walnut Creek", "Pittsburg", "South San Francisco", "Yuba City", "San Clemente", "Laguna Niguel", "Pico Rivera", "Montebello", "Lodi", "Madera", "Santa Cruz", "La Habra", "Encinitas", "Monterey Park", "Tulare", "Cupertino", "Gardena", "National City", "Rocklin", "Petaluma", "Huntington Park", "San Rafael", "La Mesa", "Arcadia", "Fountain Valley", "Diamond Bar", "Woodland", "Santee", "Lake Elsinore", "Porterville", "Paramount", "Eastvale", "Rosemead", "Hanford", "Highland", "Brentwood", "Novato", "Colton", "Cathedral City", "Delano", "Yucaipa", "Watsonville", "Placentia", "Glendora", "Gilroy", "Palm Desert", "Cerritos", "West Sacramento", "Aliso Viejo", "Poway", "La Mirada", "Rancho Santa Margarita", "Cypress", "Dublin", "Covina", "Azusa", "Palm Springs", "San Luis Obispo", "Ceres", "San Jacinto", "Lincoln", "Newark", "Lompoc", "El Centro", "Danville", "Bell Gardens", "Coachella", "Rancho Palos Verdes", "San Bruno", "Rohnert Park", "Brea", "La Puente", "Campbell", "San Gabriel", "Beaumont", "Morgan Hill", "Culver City", "Calexico", "Stanton", "La Quinta", "Pacifica", "Montclair", "Oakley", "Monrovia", "Los Banos", "Martinez" ], "Illinois": [ "Chicago", "Aurora", "Rockford", "Joliet", "Naperville", "Springfield", "Peoria", "Elgin", "Waukegan", "Cicero", "Champaign", "Bloomington", "Arlington Heights", "Evanston", "Decatur", "Schaumburg", "Bolingbrook", "Palatine", "Skokie", "Des Plaines", "Orland Park", "Tinley Park", "Oak Lawn", "Berwyn", "Mount Prospect", "Normal", "Wheaton", "Hoffman Estates", "Oak Park", "Downers Grove", "Elmhurst", "Glenview", "DeKalb", "Lombard", "Belleville", "Moline", "Buffalo Grove", "Bartlett", "Urbana", "Quincy", "Crystal Lake", "Plainfield", "Streamwood", "Carol Stream", "Romeoville", "Rock Island", "Hanover Park", "Carpentersville", "Wheeling", "Park Ridge", "Addison", "Calumet City" ], "Texas": [ "Houston", "San Antonio", "Dallas", "Austin", "Fort Worth", "El Paso", "Arlington", "Corpus Christi", "Plano", "Laredo", "Lubbock", "Garland", "Irving", "Amarillo", "Grand Prairie", "Brownsville", "Pasadena", "McKinney", "Mesquite", "McAllen", "Killeen", "Frisco", "Waco", "Carrollton", "Denton", "Midland", "Abilene", "Beaumont", "Round Rock", "Odessa", "Wichita Falls", "Richardson", "Lewisville", "Tyler", "College Station", "Pearland", "San Angelo", "Allen", "League City", "Sugar Land", "Longview", "Edinburg", "Mission", "Bryan", "Baytown", "Pharr", "Temple", "Missouri City", "Flower Mound", "Harlingen", "North Richland Hills", "Victoria", "Conroe", "New Braunfels", "Mansfield", "Cedar Park", "Rowlett", "Port Arthur", "Euless", "Georgetown", "Pflugerville", "DeSoto", "San Marcos", "Grapevine", "Bedford", "Galveston", "Cedar Hill", "Texas City", "Wylie", "Haltom City", "Keller", "Coppell", "Rockwall", "Huntsville", "Duncanville", "Sherman", "The Colony", "Burleson", "Hurst", "Lancaster", "Texarkana", "Friendswood", "Weslaco" ], "Pennsylvania": [ "Philadelphia", "Pittsburgh", "Allentown", "Erie", "Reading", "Scranton", "Bethlehem", "Lancaster", "Harrisburg", "Altoona", "York", "State College", "Wilkes-Barre" ], "Arizona": [ "Phoenix", "Tucson", "Mesa", "Chandler", "Glendale", "Scottsdale", "Gilbert", "Tempe", "Peoria", "Surprise", "Yuma", "Avondale", "Goodyear", "Flagstaff", "Buckeye", "Lake Havasu City", "Casa Grande", "Sierra Vista", "Maricopa", "Oro Valley", "Prescott", "Bullhead City", "Prescott Valley", "Marana", "Apache Junction" ], "Florida": [ "Jacksonville", "Miami", "Tampa", "Orlando", "St. Petersburg", "Hialeah", "Tallahassee", "Fort Lauderdale", "Port St. Lucie", "Cape Coral", "Pembroke Pines", "Hollywood", "Miramar", "Gainesville", "Coral Springs", "Miami Gardens", "Clearwater", "Palm Bay", "Pompano Beach", "West Palm Beach", "Lakeland", "Davie", "Miami Beach", "Sunrise", "Plantation", "Boca Raton", "Deltona", "Largo", "Deerfield Beach", "Palm Coast", "Melbourne", "Boynton Beach", "Lauderhill", "Weston", "Fort Myers", "Kissimmee", "Homestead", "Tamarac", "Delray Beach", "Daytona Beach", "North Miami", "Wellington", "North Port", "Jupiter", "Ocala", "Port Orange", "Margate", "Coconut Creek", "Sanford", "Sarasota", "Pensacola", "Bradenton", "Palm Beach Gardens", "Pinellas Park", "Coral Gables", "Doral", "Bonita Springs", "Apopka", "Titusville", "North Miami Beach", "Oakland Park", "Fort Pierce", "North Lauderdale", "Cutler Bay", "Altamonte Springs", "St. Cloud", "Greenacres", "Ormond Beach", "Ocoee", "Hallandale Beach", "Winter Garden", "Aventura" ], "Indiana": [ "Indianapolis", "Fort Wayne", "Evansville", "South Bend", "Carmel", "Bloomington", "Fishers", "Hammond", "Gary", "Muncie", "Lafayette", "Terre Haute", "Kokomo", "Anderson", "Noblesville", "Greenwood", "Elkhart", "Mishawaka", "Lawrence", "Jeffersonville", "Columbus", "Portage" ], "Ohio": [ "Columbus", "Cleveland", "Cincinnati", "Toledo", "Akron", "Dayton", "Parma", "Canton", "Youngstown", "Lorain", "Hamilton", "Springfield", "Kettering", "Elyria", "Lakewood", "Cuyahoga Falls", "Middletown", "Euclid", "Newark", "Mansfield", "Mentor", "Beavercreek", "Cleveland Heights", "Strongsville", "Dublin", "Fairfield", "Findlay", "Warren", "Lancaster", "Lima", "Huber Heights", "Westerville", "Marion", "Grove City" ] } Just need to remove the brackets and store in an array so I could iterate through and map.
You can parse the JSON and then store it in State const parsedValue = JSON.parse(JSON); setState(parsedValue)
If it's not dynamic data, I would suggest storing huge array in separate file, import it in your component and straightaway store in state.
Mongo database call returning incorrect data after reload
I am trying to sort some database entries based on a boolean representing if a job is "completed" or not. The mutation functionality is working every time according to the graphQL playground but when the database call is utilized in the application the boolean data console logs as true first but then quickly logs again as false. The playground returns the data as true every time, once it has been changed, but something inside the application is not playing nice. Me and my project partners are freshly out of a coding bootcamp and have been learning successfully at a pretty steady pace but have not been able to figure this one out. Any feedback is appreciated. Thanks!! const { loading: userLoading, data } = useQuery(QUERY_ME_BASIC); const { loading: jobsLoading, data: jobsData } = useQuery(GET_JOBS); var user = {}; var jobs = []; var completedJobs = []; var incompleteJobs = []; if (!userLoading) { console.log(jobsData) user = data.me; } if(!jobsLoading){ jobs = jobsData.jobs } if(jobs){ console.log(jobs) for (let i = 0; i < jobs.length; i++) { if (jobs[i].completed === false) { incompleteJobs.push(jobs[i]); } } } Query Response (GET_JOBS) from Graphql playground { "data": { "jobs": [ { "id": "47", "_id": "6107fce77f7afc40e4e185a7", "date": "2021-08-17", "category": "Yard Waste", "description": "addas", "distance": "1.585", "taken": true, "completed": true, "phone": "6153050246", "email": "wfh2d88#gmail.com", "driverEmail": "wfh2d88#gmail.com", "createdAt": "Aug 2nd, 2021 at 9:10 am", "pickup": { "address": "207 Porter Village Circle", "address2": "A", "city": "Nashville", "state": "Tennessee", "zip": "37206", "lat": "36.182571", "lng": "-86.733267" }, "dropoff": { "address": "1800 Stratford Ave", "address2": "A", "city": "Nashville", "state": "Alabama", "zip": "37206", "lat": "36.198164", "lng": "-86.717083" } } ] } } console.log(job) after Job Completion (3 appear one quickly after the other). First Two: 0: category: "Yard Waste" completed: true createdAt: "Aug 2nd, 2021 at 9:10 am" date: "2021-08-17" description: "addas" distance: "1.585" driverEmail: "wfh2d88#gmail.com" dropoff: {address: "1800 Stratford Ave", address2: "A", city: "Nashville", state: "Alabama", zip: "37206", …} email: "wfh2d88#gmail.com" id: "47" phone: "6153050246" pickup: {address: "207 Porter Village Circle", address2: "A", city: "Nashville", state: "Tennessee", zip: "37206", …} taken: true __typename: "Job" _id: "6107fce77f7afc40e4e185a7" [[Prototype]]: Object length: 1 [[Prototype]]: Array(0) Profile.js:32 Object Profile.js:41 Last log from same line: 0: category: "Yard Waste" completed: false createdAt: "Aug 2nd, 2021 at 9:10 am" date: "2021-08-17" description: "addas" distance: "1.585" driverEmail: "wfh2d88#gmail.com" dropoff: {address: "1800 Stratford Ave", address2: "A", city: "Nashville", state: "Alabama", zip: "37206", …} email: "wfh2d88#gmail.com" id: "47" phone: "6153050246" pickup: {address: "207 Porter Village Circle", address2: "A", city: "Nashville", state: "Tennessee", zip: "37206", …} taken: true __typename: "Job" _id: "6107fce77f7afc40e4e185a7"
how do you access every value inside the hashes within an array in json type of file?
I want to access every value of strIngredient1 key. I am not good yet at coding, but my guess is that I would have to use looping to get it, but don't really know the way to formulate it. JSON file: {"drinks":[{"strIngredient1":"Light rum"},{"strIngredient1":"Applejack"},{"strIngredient1":"Gin"},{"strIngredient1":"Dark rum"},{"strIngredient1":"Sweet Vermouth"},{"strIngredient1":"Strawberry schnapps"},{"strIngredient1":"Scotch"},{"strIngredient1":"Apricot brandy"},{"strIngredient1":"Triple sec"},{"strIngredient1":"Southern Comfort"},{"strIngredient1":"Orange bitters"},{"strIngredient1":"Brandy"},{"strIngredient1":"Lemon vodka"},{"strIngredient1":"Blended whiskey"},{"strIngredient1":"Dry Vermouth"},{"strIngredient1":"Amaretto"},{"strIngredient1":"Tea"},{"strIngredient1":"Champagne"},{"strIngredient1":"Coffee liqueur"},{"strIngredient1":"Bourbon"},{"strIngredient1":"Tequila"},{"strIngredient1":"Vodka"},{"strIngredient1":"A\u00f1ejo rum"},{"strIngredient1":"Bitters"},{"strIngredient1":"Sugar"},{"strIngredient1":"Kahlua"},{"strIngredient1":"demerara Sugar"},{"strIngredient1":"Dubonnet Rouge"},{"strIngredient1":"Watermelon"},{"strIngredient1":"Lime juice"},{"strIngredient1":"Irish whiskey"},{"strIngredient1":"Apple brandy"},{"strIngredient1":"Carbonated water"},{"strIngredient1":"Cherry brandy"},{"strIngredient1":"Creme de Cacao"},{"strIngredient1":"Grenadine"},{"strIngredient1":"Port"},{"strIngredient1":"Coffee brandy"},{"strIngredient1":"Red wine"},{"strIngredient1":"Rum"},{"strIngredient1":"Grapefruit juice"},{"strIngredient1":"Ricard"},{"strIngredient1":"Sherry"},{"strIngredient1":"Cognac"},{"strIngredient1":"Sloe gin"},{"strIngredient1":"Apple juice"},{"strIngredient1":"Pineapple juice"},{"strIngredient1":"Lemon juice"},{"strIngredient1":"Sugar syrup"},{"strIngredient1":"Milk"},{"strIngredient1":"Strawberries"},{"strIngredient1":"Chocolate syrup"},{"strIngredient1":"Yoghurt"},{"strIngredient1":"Mango"},{"strIngredient1":"Ginger"},{"strIngredient1":"Lime"},{"strIngredient1":"Cantaloupe"},{"strIngredient1":"Berries"},{"strIngredient1":"Grapes"},{"strIngredient1":"Kiwi"},{"strIngredient1":"Tomato juice"},{"strIngredient1":"Cocoa powder"},{"strIngredient1":"Chocolate"},{"strIngredient1":"Heavy cream"},{"strIngredient1":"Galliano"},{"strIngredient1":"Peach Vodka"},{"strIngredient1":"Ouzo"},{"strIngredient1":"Coffee"},{"strIngredient1":"Spiced rum"},{"strIngredient1":"Water"},{"strIngredient1":"Espresso"},{"strIngredient1":"Angelica root"},{"strIngredient1":"Orange"},{"strIngredient1":"Cranberries"},{"strIngredient1":"Johnnie Walker"},{"strIngredient1":"Apple cider"},{"strIngredient1":"Everclear"},{"strIngredient1":"Cranberry juice"},{"strIngredient1":"Egg yolk"},{"strIngredient1":"Egg"},{"strIngredient1":"Grape juice"},{"strIngredient1":"Peach nectar"},{"strIngredient1":"Lemon"},{"strIngredient1":"Firewater"},{"strIngredient1":"Lemonade"},{"strIngredient1":"Lager"},{"strIngredient1":"Whiskey"},{"strIngredient1":"Absolut Citron"},{"strIngredient1":"Pisco"},{"strIngredient1":"Irish cream"},{"strIngredient1":"Ale"},{"strIngredient1":"Chocolate liqueur"},{"strIngredient1":"Midori melon liqueur"},{"strIngredient1":"Sambuca"},{"strIngredient1":"Cider"},{"strIngredient1":"Sprite"},{"strIngredient1":"7-Up"},{"strIngredient1":"Blackberry brandy"},{"strIngredient1":"Peppermint schnapps"},{"strIngredient1":"Creme de Cassis"}]}
You could parse the json and map the values: data = {"drinks":[{"strIngredient1":"Light rum"},... parsed_json = JSON.parse(data, symbolize_names: true) strIngredient1s = parsed_json[:drinks].map { |hash| hash[:strIngredient1] }
You can do something like this: require 'json' def retrieve_values(json:) drinks_result = [] JSON.parse(json, {:symbolize_names => true})[:drinks].each do |k, v| drinks_result << v end end drinks_result The above code first parse the JSON you receive as a parameter then iterates over the list of drinks where k is the key of the sub-hashes, example: **k** -> strIngredient1 and v is the value, example: **v** -> Coffee brandy, you can use the code above as follow: retrieve_values(json: json_to_parse). The result is a list with all the values, you can iterate over that list as follow: retrieve_values(json: json_to_parse).each do |drink_i| ...execute your code here end
You have inserted newlines in your string for display purposes. If str is your string your actual strings is presumably json = str.delete("\n") Since require 'json' JSON.parse(json)["drinks"] returns an array of hashes, all of which have the single key "strIngredient1", we may write JSON.parse(json)["drinks"].map { |h| h["strIngredient1"] } or JSON.parse(json)["drinks"].flat_map(&:values) Both return the array: ["Light rum", "Applejack", "Gin", "Dark rum", "Sweet Vermouth", "Strawberry schnapps", "Scotch", "Apricot brandy", "Triple sec", "Southern Comfort", "Orange bitters", "Brandy", "Lemon vodka", "Blended whiskey", "Dry Vermouth", "Amaretto", "Tea", "Champagne", "Coffee liqueur", "Bourbon", "Tequila", "Vodka", "Añejo rum", "Bitters", "Sugar", "Kahlua", "demerara Sugar", "Dubonnet Rouge", "Watermelon", "Lime juice", "Irish whiskey", "Apple brandy", "Carbonated water", "Cherry brandy", "Creme de Cacao", "Grenadine", "Port", "Coffee brandy", "Red wine", "Rum", "Grapefruit juice", "Ricard", "Sherry", "Cognac", "Sloe gin", "Apple juice", "Pineapple juice", "Lemon juice", "Sugar syrup", "Milk", "Strawberries", "Chocolate syrup", "Yoghurt", "Mango", "Ginger", "Lime", "Cantaloupe", "Berries", "Grapes", "Kiwi", "Tomato juice", "Cocoa powder", "Chocolate", "Heavy cream", "Galliano", "Peach Vodka", "Ouzo", "Coffee", "Spiced rum", "Water", "Espresso", "Angelica root", "Orange", "Cranberries", "Johnnie Walker", "Apple cider", "Everclear", "Cranberry juice", "Egg yolk", "Egg", "Grape juice", "Peach nectar", "Lemon", "Firewater", "Lemonade", "Lager", "Whiskey", "Absolut Citron", "Pisco", "Irish cream", "Ale", "Chocolate liqueur", "Midori melon liqueur", "Sambuca", "Cider", "Sprite", "7-Up", "Blackberry brandy", "Peppermint schnapps", "Creme de Cassis"]
Does HERE have Parcel boundary data?
Does HERE have data on property parcel boundaries? I am looking for the coordinates of individual properties to overlay their maps.
Unfortunately we do not have this kind of data available. Maybe this might be interesting for you: Within the Reverse Geocode you can Request the shape of a postal district for a given latitude and longitude This example retrieves the shape and details of the first address around a specified location in Chicago (41.8839,-87.6389) using a 150 meter radius to retrieve the address. The expected address is: 425 W Randolph St, Chicago, IL 60606, United States. The addition of the additionaldata=IncludeShapeLevel,postalCode parameter ensures that the shape of the postal district is also included in the response. Reverse geocoding requests can be made using the reversegeocode endpoint and adding the prox parameter to the request URL. The number of results returned can be restricted using the maxresults parameter. https://reverse.geocoder.ls.hereapi.com/6.2/reversegeocode.json?prox=41.8839%2C-87.6389%2C150&mode=retrieveAddresses&maxresults=1&additionaldata=IncludeShapeLevel%2CpostalCode&gen=9&apiKey=xxx { "Response": { "MetaInfo": { "Timestamp": "2020-07-27T09:56:24.943+0000", "NextPageInformation": "2" }, "View": [ { "_type": "SearchResultsViewType", "ViewId": 0, "Result": [ { "Relevance": 1, "Distance": 16.3, "MatchLevel": "houseNumber", "MatchQuality": { "Country": 1, "State": 1, "County": 1, "City": 1, "District": 1, "Street": [ 1 ], "HouseNumber": 1, "PostalCode": 1 }, "MatchType": "pointAddress", "Location": { "LocationId": "NT_puy2gbuVuGd-an6zGdSyNA_xADM", "LocationType": "address", "DisplayPosition": { "Latitude": 41.88403, "Longitude": -87.63881 }, "NavigationPosition": [ { "Latitude": 41.88401, "Longitude": -87.63845 } ], "MapView": { "TopLeft": { "Latitude": 41.8851542, "Longitude": -87.6403199 }, "BottomRight": { "Latitude": 41.8829058, "Longitude": -87.6373001 } }, "Address": { "Label": "100 N Riverside Plz, Chicago, IL 60606, United States", "Country": "USA", "State": "IL", "County": "Cook", "City": "Chicago", "District": "West Loop", "Street": "N Riverside Plz", "HouseNumber": "100", "PostalCode": "60606", "AdditionalData": [ { "value": "United States", "key": "CountryName" }, { "value": "Illinois", "key": "StateName" }, { "value": "Cook", "key": "CountyName" }, { "value": "N", "key": "PostalCodeType" } ] }, "MapReference": { "ReferenceId": "1190062166", "MapId": "NAAM20117", "MapVersion": "Q1/2020", "MapReleaseDate": "2020-06-29", "Spot": 0.59, "SideOfStreet": "left", "CountryId": "21000001", "StateId": "21002247", "CountyId": "21002623", "CityId": "21002647", "BuildingId": "9000000000002726912", "AddressId": "79186499", "RoadLinkId": "499349060" }, "Shape": { "_type": "WKTShapeType", "Value": "MULTIPOLYGON (((-87.6339 41.88446, -87.6338 41.8813, -87.63239 41.88132, -87.63238 41.88067, -87.63378 41.88068, -87.63376 41.8794, -87.63377 41.87812, -87.6352 41.87811, -87.6352 41.87682, -87.63665 41.87678, -87.63663 41.87666, -87.63664 41.87658, -87.6367 41.87664, -87.63674 41.87678, -87.63706 41.87677, -87.6374 41.87807, -87.63756 41.87861, -87.63774 41.87936, -87.63794 41.88062, -87.63791 41.8819, -87.63779 41.88322, -87.63764 41.88449, -87.63727 41.88574, -87.63739 41.88602, -87.63603 41.88695, -87.63559 41.88717, -87.63248 41.8871, -87.63248 41.88703, -87.63374 41.88703, -87.63386 41.887, -87.63395 41.88702, -87.6339 41.88446)), ((-87.64102 41.87676, -87.64104 41.87804, -87.63955 41.87805, -87.63959 41.87933, -87.63966 41.88058, -87.63969 41.88187, -87.63976 41.88318, -87.6398 41.88446, -87.64022 41.88445, -87.64022 41.8846, -87.64025 41.88479, -87.64035 41.8851, -87.64047 41.88571, -87.63981 41.88572, -87.64062 41.88625, -87.64063 41.88639, -87.64064 41.88678, -87.63989 41.88679, -87.63993 41.88758, -87.6401 41.88769, -87.64035 41.88782, -87.64054 41.8879, -87.6407 41.88793, -87.64076 41.88828, -87.64085 41.88859, -87.63996 41.88847, -87.63999 41.88906, -87.63971 41.88905, -87.63961 41.88882, -87.63954 41.8887, -87.63918 41.88675, -87.63873 41.8864, -87.63841 41.88588, -87.6383 41.88573, -87.63812 41.88522, -87.63825 41.88449, -87.63845 41.88321, -87.63855 41.88231, -87.63858 41.88104, -87.63855 41.88061, -87.63836 41.87935, -87.63787 41.87794, -87.63778 41.87751, -87.63752 41.87751, -87.63752 41.87731, -87.63775 41.87728, -87.6377 41.87687, -87.63784 41.87684, -87.63778 41.87676, -87.64102 41.87676)))" } } } ] } ] } } See also https://developer.here.com/blog/how-to-get-the-shape-of-an-area-using-the-here-geocoder-api
Discord.js - JSON Parsing?
Okay, I'm a bit new to JSON in Discord.js Bot Development, And I'm currently making a command where it grabs all of the data in a json file and splits it so it looks nicely formatted. Currently have: let Games = { "1": "Jailbreak", "2": "Adopt Me", "3": "Bubble Gum Simulator", "4": "Thick Legends", "5": "Arsenal", "6": "Legends Of Speed", "7": "Speed Champions", "8": "Build A Boat For Treasure", "9": "Boxing Simulator", "10": "Flight Simulator", "11": "Mad City", "12": "Redwood Prison", "13": "Horrific Housing", "14": "Welcome To Bloxburg", "15": "Tower Of Hell" } function getGames(lol) { lol = JSON.parse(Games[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]); for (let j in lol) { return "• " + lol[j]; } } I want to group all of the Games and make it output: "• example\n" Any answers?
You can get all object values, use map to add • and join it with '\n'. Like this: let Games = { "1": "Jailbreak", "2": "Adopt Me", "3": "Bubble Gum Simulator", "4": "Thick Legends", "5": "Arsenal", "6": "Legends Of Speed", "7": "Speed Champions", "8": "Build A Boat For Treasure", "9": "Boxing Simulator", "10": "Flight Simulator", "11": "Mad City", "12": "Redwood Prison", "13": "Horrific Housing", "14": "Welcome To Bloxburg", "15": "Tower Of Hell" } console.log(getGames(Games)) function getGames(gameList) { return Object.values(gameList).map(game => `• ${game}`).join('\n') } Live example let Games = { "1": "Jailbreak", "2": "Adopt Me", "3": "Bubble Gum Simulator", "4": "Thick Legends", "5": "Arsenal", "6": "Legends Of Speed", "7": "Speed Champions", "8": "Build A Boat For Treasure", "9": "Boxing Simulator", "10": "Flight Simulator", "11": "Mad City", "12": "Redwood Prison", "13": "Horrific Housing", "14": "Welcome To Bloxburg", "15": "Tower Of Hell" } console.log(getGames(Games)) function getGames(gameList) { return Object.values(gameList).map(game => `• ${game}`).join('\n') }
You can get an array of the object values with Object.values() let gamenames = Object.values(games) Then,you can loop through the array elements to add them to a string. let myString = "List: \n" gamenames.forEach((val) => { myString += `• ${val} \n` }) myString is now equal to an organized list of the games! It should look like: List: • Jailbreak • Adopt me ...etc.. (The list does put every single value on a seperate line,but StackOverflow doesn't want to show them properly for some reason) Hope this helped! If it did,please mark my question as a valid answer.