Is there a way I can get the 3 char country code in Google maps’s Reverse Geocoding?
The below query returns the 2 char country code (ISO 3166-1 encoding). But I need to have the 3 char country code.
HTML:
<html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"> </head> <body> <div id="locationField"> <input id="autocomplete" placeholder="Enter your address" type="text" /> </div> <table id="address"> <tr> <td class="label">City</td> <td class="wideField" colspan="3"> <input class="field" id="locality" disabled="true" /> </td> </tr> <tr> <td class="label">State</td> <td class="slimField"> <input class="field" id="administrative_area_level_1" disabled="true" /> </td> </tr> <tr> <td class="label">Country</td> <td class="wideField" colspan="2"> <input class="field" id="country" disabled="true" /> </td> <td class="label">Country Code</td> <td class="shortField"> <input class="field" id="country_code" disabled="true" /> </td> </tr> <tr> <td class="label">latitude</td> <td class="wideField" colspan="3"> <input class="field" id="lat" disabled="true" /> </td> </tr> <tr> <td class="label">longitude</td> <td class="wideField" colspan="3"> <input class="field" id="lng" disabled="true" /> </td> </tr> </table> </body> </html>
Javascript:
// This example displays an address form, using the autocomplete feature // of the Google Places API to help users fill in the information. function fillInAddress() { // country code array with two letter and thre letter. var countries = [ {"two" :"AF","three" : "AFG" }, {"two" :"AL","three" : "ALB" }, {"two" :"DZ","three" : "DZA" }, {"two" :"AS","three" : "ASM" }, {"two" :"AD","three" : "AND" }, {"two" :"AO","three" : "AGO" }, {"two" :"AI","three" : "AIA" }, {"two" :"AQ","three" : "ATA" }, {"two" :"AG","three" : "ATG" }, {"two" :"AR","three" : "ARG" }, {"two" :"AM","three" : "ARM" }, {"two" :"AW","three" : "ABW" }, {"two" :"AU","three" : "AUS" }, {"two" :"AT","three" : "AUT" }, {"two" :"AZ","three" : "AZE" }, {"two" :"BS","three" : "BHS" }, {"two" :"BH","three" : "BHR" }, {"two" :"BD","three" : "BGD" }, {"two" :"BB","three" : "BRB" }, {"two" :"BY","three" : "BLR" }, {"two" :"BE","three" : "BEL" }, {"two" :"BZ","three" : "BLZ" }, {"two" :"BJ","three" : "BEN" }, {"two" :"BM","three" : "BMU" }, {"two" :"BT","three" : "BTN" }, {"two" :"BO","three" : "BOL" }, {"two" :"BO","three" : "BOL" }, {"two" :"BA","three" : "BIH" }, {"two" :"BW","three" : "BWA" }, {"two" :"BV","three" : "BVT" }, {"two" :"BR","three" : "BRA" }, {"two" :"IO","three" : "IOT" }, {"two" :"BN","three" : "BRN" }, {"two" :"BN","three" : "BRN" }, {"two" :"BG","three" : "BGR" }, {"two" :"BF","three" : "BFA" }, {"two" :"BI","three" : "BDI" }, {"two" :"KH","three" : "KHM" }, {"two" :"CM","three" : "CMR" }, {"two" :"CA","three" : "CAN" }, {"two" :"CV","three" : "CPV" }, {"two" :"KY","three" : "CYM" }, {"two" :"CF","three" : "CAF" }, {"two" :"TD","three" : "TCD" }, {"two" :"CL","three" : "CHL" }, {"two" :"CN","three" : "CHN" }, {"two" :"CX","three" : "CXR" }, {"two" :"CC","three" : "CCK" }, {"two" :"CO","three" : "COL" }, {"two" :"KM","three" : "COM" }, {"two" :"CG","three" : "COG" }, {"two" :"CD","three" : "COD" }, {"two" :"CK","three" : "COK" }, {"two" :"CR","three" : "CRI" }, {"two" :"CI","three" : "CIV" }, {"two" :"CI","three" : "CIV" }, {"two" :"HR","three" : "HRV" }, {"two" :"CU","three" : "CUB" }, {"two" :"CY","three" : "CYP" }, {"two" :"CZ","three" : "CZE" }, {"two" :"DK","three" : "DNK" }, {"two" :"DJ","three" : "DJI" }, {"two" :"DM","three" : "DMA" }, {"two" :"DO","three" : "DOM" }, {"two" :"EC","three" : "ECU" }, {"two" :"EG","three" : "EGY" }, {"two" :"SV","three" : "SLV" }, {"two" :"GQ","three" : "GNQ" }, {"two" :"ER","three" : "ERI" }, {"two" :"EE","three" : "EST" }, {"two" :"ET","three" : "ETH" }, {"two" :"FK","three" : "FLK" }, {"two" :"FO","three" : "FRO" }, {"two" :"FJ","three" : "FJI" }, {"two" :"FI","three" : "FIN" }, {"two" :"FR","three" : "FRA" }, {"two" :"GF","three" : "GUF" }, {"two" :"PF","three" : "PYF" }, {"two" :"TF","three" : "ATF" }, {"two" :"GA","three" : "GAB" }, {"two" :"GM","three" : "GMB" }, {"two" :"GE","three" : "GEO" }, {"two" :"DE","three" : "DEU" }, {"two" :"GH","three" : "GHA" }, {"two" :"GI","three" : "GIB" }, {"two" :"GR","three" : "GRC" }, {"two" :"GL","three" : "GRL" }, {"two" :"GD","three" : "GRD" }, {"two" :"GP","three" : "GLP" }, {"two" :"GU","three" : "GUM" }, {"two" :"GT","three" : "GTM" }, {"two" :"GG","three" : "GGY" }, {"two" :"GN","three" : "GIN" }, {"two" :"GW","three" : "GNB" }, {"two" :"GY","three" : "GUY" }, {"two" :"HT","three" : "HTI" }, {"two" :"HM","three" : "HMD" }, {"two" :"VA","three" : "VAT" }, {"two" :"HN","three" : "HND" }, {"two" :"HK","three" : "HKG" }, {"two" :"HU","three" : "HUN" }, {"two" :"IS","three" : "ISL" }, {"two" :"IN","three" : "IND" }, {"two" :"ID","three" : "IDN" }, {"two" :"IR","three" : "IRN" }, {"two" :"IQ","three" : "IRQ" }, {"two" :"IE","three" : "IRL" }, {"two" :"IM","three" : "IMN" }, {"two" :"IL","three" : "ISR" }, {"two" :"IT","three" : "ITA" }, {"two" :"JM","three" : "JAM" }, {"two" :"JP","three" : "JPN" }, {"two" :"JE","three" : "JEY" }, {"two" :"JO","three" : "JOR" }, {"two" :"KZ","three" : "KAZ" }, {"two" :"KE","three" : "KEN" }, {"two" :"KI","three" : "KIR" }, {"two" :"KP","three" : "PRK" }, {"two" :"KR","three" : "KOR" }, {"two" :"KR","three" : "KOR" }, {"two" :"KW","three" : "KWT" }, {"two" :"KG","three" : "KGZ" }, {"two" :"LA","three" : "LAO" }, {"two" :"LV","three" : "LVA" }, {"two" :"LB","three" : "LBN" }, {"two" :"LS","three" : "LSO" }, {"two" :"LR","three" : "LBR" }, {"two" :"LY","three" : "LBY" }, {"two" :"LY","three" : "LBY" }, {"two" :"LI","three" : "LIE" }, {"two" :"LT","three" : "LTU" }, {"two" :"LU","three" : "LUX" }, {"two" :"MO","three" : "MAC" }, {"two" :"MK","three" : "MKD" }, {"two" :"MG","three" : "MDG" }, {"two" :"MW","three" : "MWI" }, {"two" :"MY","three" : "MYS" }, {"two" :"MV","three" : "MDV" }, {"two" :"ML","three" : "MLI" }, {"two" :"MT","three" : "MLT" }, {"two" :"MH","three" : "MHL" }, {"two" :"MQ","three" : "MTQ" }, {"two" :"MR","three" : "MRT" }, {"two" :"MU","three" : "MUS" }, {"two" :"YT","three" : "MYT" }, {"two" :"MX","three" : "MEX" }, {"two" :"FM","three" : "FSM" }, {"two" :"MD","three" : "MDA" }, {"two" :"MC","three" : "MCO" }, {"two" :"MN","three" : "MNG" }, {"two" :"ME","three" : "MNE" }, {"two" :"MS","three" : "MSR" }, {"two" :"MA","three" : "MAR" }, {"two" :"MZ","three" : "MOZ" }, {"two" :"MM","three" : "MMR" }, {"two" :"MM","three" : "MMR" }, {"two" :"NA","three" : "NAM" }, {"two" :"NR","three" : "NRU" }, {"two" :"NP","three" : "NPL" }, {"two" :"NL","three" : "NLD" }, {"two" :"AN","three" : "ANT" }, {"two" :"NC","three" : "NCL" }, {"two" :"NZ","three" : "NZL" }, {"two" :"NI","three" : "NIC" }, {"two" :"NE","three" : "NER" }, {"two" :"NG","three" : "NGA" }, {"two" :"NU","three" : "NIU" }, {"two" :"NF","three" : "NFK" }, {"two" :"MP","three" : "MNP" }, {"two" :"NO","three" : "NOR" }, {"two" :"OM","three" : "OMN" }, {"two" :"PK","three" : "PAK" }, {"two" :"PW","three" : "PLW" }, {"two" :"PS","three" : "PSE" }, {"two" :"PA","three" : "PAN" }, {"two" :"PG","three" : "PNG" }, {"two" :"PY","three" : "PRY" }, {"two" :"PE","three" : "PER" }, {"two" :"PH","three" : "PHL" }, {"two" :"PN","three" : "PCN" }, {"two" :"PL","three" : "POL" }, {"two" :"PT","three" : "PRT" }, {"two" :"PR","three" : "PRI" }, {"two" :"QA","three" : "QAT" }, {"two" :"RE","three" : "REU" }, {"two" :"RO","three" : "ROU" }, {"two" :"RU","three" : "RUS" }, {"two" :"RU","three" : "RUS" }, {"two" :"RW","three" : "RWA" }, {"two" :"SH","three" : "SHN" }, {"two" :"KN","three" : "KNA" }, {"two" :"LC","three" : "LCA" }, {"two" :"PM","three" : "SPM" }, {"two" :"VC","three" : "VCT" }, {"two" :"VC","three" : "VCT" }, {"two" :"VC","three" : "VCT" }, {"two" :"WS","three" : "WSM" }, {"two" :"SM","three" : "SMR" }, {"two" :"ST","three" : "STP" }, {"two" :"SA","three" : "SAU" }, {"two" :"SN","three" : "SEN" }, {"two" :"RS","three" : "SRB" }, {"two" :"SC","three" : "SYC" }, {"two" :"SL","three" : "SLE" }, {"two" :"SG","three" : "SGP" }, {"two" :"SK","three" : "SVK" }, {"two" :"SI","three" : "SVN" }, {"two" :"SB","three" : "SLB" }, {"two" :"SO","three" : "SOM" }, {"two" :"ZA","three" : "ZAF" }, {"two" :"GS","three" : "SGS" }, {"two" :"ES","three" : "ESP" }, {"two" :"LK","three" : "LKA" }, {"two" :"SD","three" : "SDN" }, {"two" :"SR","three" : "SUR" }, {"two" :"SJ","three" : "SJM" }, {"two" :"SZ","three" : "SWZ" }, {"two" :"SE","three" : "SWE" }, {"two" :"CH","three" : "CHE" }, {"two" :"SY","three" : "SYR" }, {"two" :"TW","three" : "TWN" }, {"two" :"TW","three" : "TWN" }, {"two" :"TJ","three" : "TJK" }, {"two" :"TZ","three" : "TZA" }, {"two" :"TH","three" : "THA" }, {"two" :"TL","three" : "TLS" }, {"two" :"TG","three" : "TGO" }, {"two" :"TK","three" : "TKL" }, {"two" :"TO","three" : "TON" }, {"two" :"TT","three" : "TTO" }, {"two" :"TT","three" : "TTO" }, {"two" :"TN","three" : "TUN" }, {"two" :"TR","three" : "TUR" }, {"two" :"TM","three" : "TKM" }, {"two" :"TC","three" : "TCA" }, {"two" :"TV","three" : "TUV" }, {"two" :"UG","three" : "UGA" }, {"two" :"UA","three" : "UKR" }, {"two" :"AE","three" : "ARE" }, {"two" :"GB","three" : "GBR" }, {"two" :"US","three" : "USA" }, {"two" :"UM","three" : "UMI" }, {"two" :"UY","three" : "URY" }, {"two" :"UZ","three" : "UZB" }, {"two" :"VU","three" : "VUT" }, {"two" :"VE","three" : "VEN" }, {"two" :"VE","three" : "VEN" }, {"two" :"VN","three" : "VNM" }, {"two" :"VN","three" : "VNM" }, {"two" :"VG","three" : "VGB" }, {"two" :"VI","three" : "VIR" }, {"two" :"WF","three" : "WLF" }, {"two" :"EH","three" : "ESH" }, {"two" :"YE","three" : "YEM" }, {"two" :"ZM","three" : "ZMB" }, {"two" :"ZW","three" : "ZWE" } ];//etc // Get the place details from the autocomplete object. var place = autocomplete.getPlace(); for (var component in componentForm) { document.getElementById(component).value = ''; } // Get each component of the address from the place details // and fill the corresponding field on the form. for (var i = 0; i < place.address_components.length; i++) { var addressType = place.address_components[i].types[0]; if (componentForm[addressType]) { var val = place.address_components[i][componentForm[addressType]]; document.getElementById(addressType).value = val; } // for the country, get the country code (the "short name") also if (addressType == "country") { console.log(getKeyByParameter(countries, place.address_components[i].short_name)); document.getElementById("country_code").value = getKeyByParameter(countries, place.address_components[i].short_name); } } } var placeSearch, autocomplete; var componentForm = { locality: 'long_name', administrative_area_level_1: 'short_name', country: 'long_name', }; function initAutocomplete() { // Create the autocomplete object, restricting the search to geographical // location types. autocomplete = new google.maps.places.Autocomplete( /** @type {!HTMLInputElement} */ (document.getElementById('autocomplete')), { types: ['(cities)'] }); // When the user selects an address from the dropdown, populate the address // fields in the form. // Get Latitude and longitude google.maps.event.addListener(autocomplete, 'place_changed', function() { var place = autocomplete.getPlace(); console.log(place); document.getElementById('lat').value = place.geometry.location.lat(); document.getElementById('lng').value = place.geometry.location.lng(); fillInAddress(); }); } google.maps.event.addDomListener(window, 'load', initAutocomplete); // function for revers coding var getKeyByParameter = function(obj, parameter) { var returnVal = ""; jQuery.each(obj, function(key, info) { if (info.two == parameter) { returnVal = info.three; return false; }; }); return returnVal; }
No comments:
Post a Comment