I am building a web app that requires to store the location of a client along with the phone number. I want the phone code to be auto-detected after location is selected. I have gone through the documentation of google maps provided in the following link but with no luck. Google Maps Documentation . Is there any way to detect phone code from google maps location?
Asked
Active
Viewed 2,462 times
-2
-
Do you mean country phone code, or local codes also? – bcperth Oct 25 '18 at 08:43
-
1i mean only the country phone code – Bonish Koirala Oct 25 '18 at 09:11
-
Is the API I gave below in reply OK? – bcperth Oct 25 '18 at 09:17
-
the api is okay but only provides result for nepal's phonecode – Bonish Koirala Oct 25 '18 at 10:49
1 Answers
1
Once you have identified the country you can go to an external API like https://restcountries.eu
and retrieve data (including phone code) about any country.
For example https://restcountries.eu/rest/v2/name/nepal?fullText=true
returns this JSON, where you can see the callingCodes = 997
.
[{
"name": "Nepal",
"topLevelDomain": [".np"],
"alpha2Code": "NP",
"alpha3Code": "NPL",
"callingCodes": ["977"],
"capital": "Kathmandu",
"altSpellings": ["NP", "Federal Democratic Republic of Nepal", "Loktāntrik Ganatantra Nepāl"],
"region": "Asia",
"subregion": "Southern Asia",
"population": 28431500,
"latlng": [28.0, 84.0],
"demonym": "Nepalese",
"area": 147181.0,
"gini": 32.8,
"timezones": ["UTC+05:45"],
"borders": ["CHN", "IND"],
"nativeName": "नेपाल",
"numericCode": "524",
"currencies": [{
"code": "NPR",
"name": "Nepalese rupee",
"symbol": "₨"
}],
"languages": [{
"iso639_1": "ne",
"iso639_2": "nep",
"name": "Nepali",
"nativeName": "नेपाली"
}],
"translations": {
"de": "Népal",
"es": "Nepal",
"fr": "Népal",
"ja": "ネパール",
"it": "Nepal",
"br": "Nepal",
"pt": "Nepal",
"nl": "Nepal",
"hr": "Nepal",
"fa": "نپال"
},
"flag": "https://restcountries.eu/data/npl.svg",
"regionalBlocs": [{
"acronym": "SAARC",
"name": "South Asian Association for Regional Cooperation",
"otherAcronyms": [],
"otherNames": []
}],
"cioc": "NEP"
}]
EDIT: Code added on request.
<html>
<body>
<h2>Get the phone code for Napal</h2>
<button type="button" onclick="loadDoc()">Get Phone Code</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var locObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = "Phone Code for Napal = "+locObj[0].callingCodes;
}
};
xhttp.open("GET", "https://restcountries.eu/rest/v2/name/nepal?fullText=true", true);
xhttp.send();
}
</script>
</body>
</html>
Produces this output:
Phone Code for Napal = 977

bcperth
- 2,191
- 1
- 10
- 16
-
so how do i extract the calling code from this response information? – Bonish Koirala Oct 25 '18 at 09:47
-
Are you working in PHP or Javascript? The API returns a JSON array that can be parsed easily in those languages. – bcperth Oct 25 '18 at 09:51
-
Ok I just posted some Javascript to show you how to extract the code from the JSON. You can put this in a html file and your browser can execute it. – bcperth Oct 25 '18 at 10:10
-
-
for some reason this solution is only giving back the result of nepal as 977. for every other region it results in 246. i am surprised how easily people degrade a solution (whoever did that). some solutions and questions may be the exact thing people are looking for. – Bonish Koirala Oct 25 '18 at 10:48
-
I just changed one line to `https://restcountries.eu/rest/v2/name/ireland` and it correctly returned 353. Show me example which results in 246? – bcperth Oct 25 '18 at 10:54
-
-
You are right. The India data seems wrong! I will check it some more ... maybe others are wrong also. – bcperth Oct 25 '18 at 11:00
-
Ok Is fixed, See code above. You need to add `?fullText=true` as in `https://restcountries.eu/rest/v2/name/india?fullText=true`. Otherwise the response will include other countries that contain the search string. – bcperth Oct 25 '18 at 11:09