Javascript 获取用户位置(国家、语言、时区)
Posted
技术标签:
【中文标题】Javascript 获取用户位置(国家、语言、时区)【英文标题】:Javascript get users location(country, lang, timezone) 【发布时间】:2021-01-04 10:43:56 【问题描述】:我想收集所有可以从用户那里获取的信息,而无需询问任何权限。 我不想使用任何非免费的 ip 信息服务和地理定位
我现在拥有的:
timeZoneOffset = new Date().getTimezoneOffset();
userLang = navigator.language || navigator.userLanguage;
但我想要更多关于用户位置的信息。
【问题讨论】:
这是不可能的 你可以免费试用这个以获得更准确的时区***.com/a/63915192/295783 【参考方案1】:解决方案 1:html Geolocation API 用于获取用户的地理位置。
但它请求用户许可。解决方案 2:免费 IP 定位 API
检查这个:https://ipdata.co/
$.getJSON('https://geolocation-db.com/json/')
.done (function(location)
$('#country').html(location.country_name);
$('#state').html(location.state);
$('#city').html(location.city);
$('#latitude').html(location.latitude);
$('#longitude').html(location.longitude);
$('#ip').html(location.IPv4);
);
解决方案 3:基于付费 IP 的 API
对此没有其他解决方案。
【讨论】:
【参考方案2】:其他答案提到付费服务 (ipdata) 或没有您需要的数据的免费服务 - 时区等 - (geolocation-db.com)
我相信 Abstract 为其 IP 地理定位 API 提供免费计划,其中包括时区数据:https://www.abstractapi.com/ip-geolocation-api
$.getJSON("https://ipgeolocation.abstractapi.com/v1/?api_key=****************", function(data)
console.log(data.ip_address);
console.log(data.country);
console.log(data.timezone);
)
【讨论】:
【参考方案3】:您可以使用 BigDataCloud 的 IP Geolocation API 从您的网站访问者的 IP 地址获取最大数据。
它每月免费提供 10K 查询。
https://www.bigdatacloud.com/ip-geolocation-apis/ip-address-geolocation-api
输出数据:
"ip": "73.231.131.73",
"localityLanguageRequested": "en",
"isReachableGlobally": true,
"country":
"isoAlpha2": "US",
"isoAlpha3": "USA",
"m49Code": 840,
"name": "United States of America",
"isoName": "United States of America (the)",
"isoNameFull": "the United States of America",
"isoAdminLanguages": [
"isoAlpha3": "eng",
"isoAlpha2": "en",
"isoName": "English",
"nativeName": "English"
],
"unRegion": "Americas/Northern America",
"currency":
"numericCode": 840,
"code": "USD",
"name": "US Dollar",
"minorUnits": 2
,
"wbRegion":
"id": "NAC",
"iso2Code": "XU",
"value": "North America"
,
"wbIncomeLevel":
"id": "HIC",
"iso2Code": "XD",
"value": "High income"
,
"callingCode": "1",
"countryFlagEmoji": "??"
,
"location":
"continent": "North America",
"continentCode": "NA",
"isoPrincipalSubdivision": "California",
"isoPrincipalSubdivisionCode": "US-CA",
"city": "Milpitas",
"localityName": "Milpitas",
"postcode": "95035",
"latitude": 37.45,
"longitude": -121.92,
"plusCode": "849WF32J+22",
"timeZone":
"ianaTimeId": "America/Los_Angeles",
"displayName": "(UTC-08:00) Pacific Standard Time",
"effectiveTimeZoneFull": "Pacific Standard Time",
"effectiveTimeZoneShort": "PST",
"utcOffsetSeconds": -28800,
"utcOffset": "-08",
"isDaylightSavingTime": false,
"localTime": "2021-02-28T17:11:59.1873233"
,
"localityInfo":
"administrative": [
"order": 2,
"adminLevel": 2,
"name": "United States of America",
"description": "country in North America",
"isoName": "United States of America (the)",
"isoCode": "US",
"wikidataId": "Q30",
"geonameId": 6252001
,
"order": 4,
"adminLevel": 4,
"name": "California",
"description": "state of the United States of America",
"isoName": "California",
"isoCode": "US-CA",
"wikidataId": "Q99",
"geonameId": 5332921
,
"order": 6,
"adminLevel": 6,
"name": "Santa Clara County",
"description": "county in California, United States",
"wikidataId": "Q110739",
"geonameId": 5393021
,
"order": 8,
"adminLevel": 8,
"name": "Milpitas",
"description": "city in Santa Clara County, California",
"wikidataId": "Q927510",
"geonameId": 5373327
],
"informative": [
"order": 1,
"name": "North America",
"description": "continent on the Earth's northwestern quadrant",
"isoCode": "NA",
"wikidataId": "Q49",
"geonameId": 6255149
,
"order": 3,
"name": "contiguous United States",
"description": "48 states of the United States apart from Alaska and Hawaii",
"wikidataId": "Q578170"
,
"order": 5,
"name": "Pacific Coast Ranges",
"description": "A series of mountain ranges along the Pacific coast of North America",
"wikidataId": "Q660304"
,
"order": 7,
"name": "95035",
"description": "postal code"
]
,
"lastUpdated": "2021-02-28T23:39:21.8284029Z",
"network":
"registry": "ARIN",
"registryStatus": "assigned",
"registeredCountry": "US",
"registeredCountryName": "United States of America",
"organisation": "Comcast Cable Communications, LLC",
"isReachableGlobally": true,
"isBogon": false,
"bgpPrefix": "73.231.0.0/16",
"bgpPrefixNetworkAddress": "73.231.0.0",
"bgpPrefixLastAddress": "73.231.255.255",
"totalAddresses": 65536,
"carriers": [
"asn": "AS33651",
"asnNumeric": 33651,
"organisation": "Comcast Cable Communications LLC",
"name": "CMCS",
"registry": "ARIN",
"registeredCountry": "US",
"registeredCountryName": "United States of America",
"registrationDate": "2005-02-17",
"registrationLastChange": "2021-01-26",
"totalIpv4Addresses": 3147776,
"totalIpv4Prefixes": 217,
"totalIpv4BogonPrefixes": 0,
"rank": 152,
"rankText": "#152 out of 70,908"
],
"viaCarriers": [
"asn": "AS7922",
"asnNumeric": 7922,
"organisation": "Comcast Cable Communications LLC",
"registeredCountry": "US",
"registeredCountryName": "United States of America",
"totalIpv4Addresses": 24911902,
"rank": 15
]
,
"confidence": "high",
"confidenceArea": [
"latitude": 37.41,
"longitude": -122.244804
,
"latitude": 37.42,
"longitude": -122.244804
,
"latitude": 37.483982,
"longitude": -122.23432
,
"latitude": 37.513985,
"longitude": -122.22432
,
"latitude": 37.557896,
"longitude": -122.20398
,
"latitude": 37.598026,
"longitude": -122.17369
,
"latitude": 37.632973,
"longitude": -122.134514
,
"latitude": 37.66151,
"longitude": -122.08782
,
"latitude": 37.68264,
"longitude": -122.03526
,
"latitude": 37.69562,
"longitude": -121.97865
,
"latitude": 37.7,
"longitude": -121.92
,
"latitude": 37.7,
"longitude": -121.89
,
"latitude": 37.6962,
"longitude": -121.83531
,
"latitude": 37.684914,
"longitude": -121.782295
,
"latitude": 37.666485,
"longitude": -121.732544
,
"latitude": 37.64147,
"longitude": -121.687584
,
"latitude": 37.62147,
"longitude": -121.657585
,
"latitude": 37.582153,
"longitude": -121.610214
,
"latitude": 37.535625,
"longitude": -121.57468
,
"latitude": 37.484097,
"longitude": -121.55265
,
"latitude": 37.43,
"longitude": -121.5452
,
"latitude": 37.42,
"longitude": -121.5452
,
"latitude": 37.364517,
"longitude": -121.55305
,
"latitude": 37.311806,
"longitude": -121.5762
,
"latitude": 37.26449,
"longitude": -121.61351
,
"latitude": 37.25449,
"longitude": -121.62351
,
"latitude": 37.22382,
"longitude": -121.65991
,
"latitude": 37.19844,
"longitude": -121.70228
,
"latitude": 37.188442,
"longitude": -121.722275
,
"latitude": 37.16475,
"longitude": -121.78348
,
"latitude": 37.15206,
"longitude": -121.84966
,
"latitude": 37.150974,
"longitude": -121.91773
,
"latitude": 37.161533,
"longitude": -121.98451
,
"latitude": 37.17153,
"longitude": -122.02451
,
"latitude": 37.17153,
"longitude": -122.02451
,
"latitude": 37.19254,
"longitude": -122.085304
,
"latitude": 37.223186,
"longitude": -122.1392
,
"latitude": 37.262123,
"longitude": -122.18382
,
"latitude": 37.307613,
"longitude": -122.21719
,
"latitude": 37.357647,
"longitude": -122.23782
,
"latitude": 37.41,
"longitude": -122.244804
],
"securityThreat": "unknown",
"hazardReport":
"isKnownAsTorServer": false,
"isKnownAs***": false,
"isKnownAsProxy": false,
"isSpamhausDrop": false,
"isSpamhausEdrop": false,
"isSpamhausAsnDrop": false,
"isBlacklistedUceprotect": false,
"isBlacklistedBlocklistDe": false,
"isKnownAsMailServer": false,
"isKnownAsPublicRouter": false,
"isBogon": false,
"isUnreachable": false,
"hostingLikelihood": 0,
"isHostingAsn": false,
"isCellular": false
【讨论】:
以上是关于Javascript 获取用户位置(国家、语言、时区)的主要内容,如果未能解决你的问题,请参考以下文章
当用户在 Google 地图中拒绝位置访问时,如何向用户显示当前国家/地区?
如何使用地理位置而不是完整的国家和地区名称获取国家和地区代码
如何使用 Vanilla JavaScript/Ajax 获取地理位置 - 国家和城市