有什么方法可以从 google 地图自动完成 api 中检测国家 phone 代码吗?
Is there any way to detect country phone code from google maps autocomplete api?
我正在构建一个 Web 应用程序,它需要存储客户端的位置以及 phone 号码。我希望在选择位置后自动检测 phone 代码。我已经阅读了以下 link 中提供的 google 地图的文档,但没有成功。
Google Maps Documentation
.有什么方法可以从 google 地图位置检测 phone 代码吗?
一旦您确定了国家/地区,您就可以转到外部 API,例如 https://restcountries.eu
并检索有关任何国家/地区的数据(包括 phone 代码)。
比如https://restcountries.eu/rest/v2/name/nepal?fullText=true
returns这个JSON,在这里你可以看到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"
}]
编辑: 根据要求添加代码。
<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>
产生这个输出:
Phone Code for Napal = 977
我正在构建一个 Web 应用程序,它需要存储客户端的位置以及 phone 号码。我希望在选择位置后自动检测 phone 代码。我已经阅读了以下 link 中提供的 google 地图的文档,但没有成功。 Google Maps Documentation .有什么方法可以从 google 地图位置检测 phone 代码吗?
一旦您确定了国家/地区,您就可以转到外部 API,例如 https://restcountries.eu
并检索有关任何国家/地区的数据(包括 phone 代码)。
比如https://restcountries.eu/rest/v2/name/nepal?fullText=true
returns这个JSON,在这里你可以看到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"
}]
编辑: 根据要求添加代码。
<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>
产生这个输出:
Phone Code for Napal = 977