Javascript 获取页面加载时的位置
我正在建立一个网站使用HTML,CSS和Java脚本。我想在页面加载时获取用户的位置。因此,他们不需要按下按钮。我希望有人能帮忙Javascript 获取页面加载时的位置,javascript,css,html,geolocation,Javascript,Css,Html,Geolocation,我正在建立一个网站使用HTML,CSS和Java脚本。我想在页面加载时获取用户的位置。因此,他们不需要按下按钮。我希望有人能帮忙 !DOCTYPE html> <html> <body> <p id="demo">Click the button to get your coordinates:</p> <button onclick="getLocation()">Try It</button> <script
!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
var x=document.getElementById("demo");
function getLocation(){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}
else{
x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position){
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
!DOCTYPE html>
单击按钮获取您的坐标:
试试看
var x=document.getElementById(“演示”);
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showPosition);
}
否则{
x、 innerHTML=“此浏览器不支持地理位置。”;}
}
功能显示位置(位置){
x、 innerHTML=“纬度:”+position.coords.Latitude+
“
经度:”+position.coords.Longitude;
}
只需调用getLocation()
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
getLocation()
</script>
var x=document.getElementById(“演示”);
函数getLocation()
{
if(导航器.地理位置)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML=“此浏览器不支持地理位置。”;}
}
功能显示位置(位置)
{
x、 innerHTML=“纬度:”+position.coords.Latitude+
“
经度:”+position.coords.Longitude;
}
getLocation()
小提琴:
var x=document.getElementById(“演示”);
函数getLocation()
{
if(导航器.地理位置)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML=“此浏览器不支持地理位置。”;}
}
功能显示位置(位置)
{
x、 innerHTML=“纬度:”+position.coords.Latitude+
“
经度:”+position.coords.Longitude;
}
getLocation()
这是对上述答案的更新,适用于希望在react中执行此操作而不添加其他脚本的任何人。以下是ES2015格式的代码,用于React等,无需使用上述任何HTML内容。只需导入函数。
特别感谢第一篇文章,因为我是基于它写的
const showPosition = position =>{
let loc = `Latitude:${position.coords.latitude} logitude:
${position.coords.longitude}`
console.log(loc)
return loc
}
const getLocation = () => {
if (navigator.geolocation) {
return navigator.geolocation.getCurrentPosition(showPosition);
}
return false
}
然后导入函数并使用它
如果你想得到状态和压缩,你可以使用谷歌API。这是获取地理位置的代码,发送到google,然后给出状态、地址和结果。您需要激活google地理定位API
//Docs - https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding
const getData = async (url) => {
let res = await fetch(url)
let json = await res.json()
console.log(json);
return json
}
const showPosition = async position =>{
let loc = `Latitude:${position.coords.latitude} logitude: ${position.coords.longitude}`
let url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${position.coords.latitude},${position.coords.longitude}&key={GOOGLE_API_key}`
console.log(loc)
let address = await getData(url)
console.log(`results`,address)
return address
}
const getLocation = async () => {
if (navigator.geolocation) {
return navigator.geolocation.getCurrentPosition(showPosition);
}
return false
}
问题不清楚,请详细说明。以上代码当你打开一个页面,你有一个按钮按下,然后弹出窗口会问你,如果网站可以使用你的位置。你说可以。我想错过第一步,所以当你们打开网站,然后直接问你们网站是否可以使用你们的位置。只是想错过第一个按钮阶段。从Chrome版本50开始,该网站必须运行https。
//Docs - https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding
const getData = async (url) => {
let res = await fetch(url)
let json = await res.json()
console.log(json);
return json
}
const showPosition = async position =>{
let loc = `Latitude:${position.coords.latitude} logitude: ${position.coords.longitude}`
let url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${position.coords.latitude},${position.coords.longitude}&key={GOOGLE_API_key}`
console.log(loc)
let address = await getData(url)
console.log(`results`,address)
return address
}
const getLocation = async () => {
if (navigator.geolocation) {
return navigator.geolocation.getCurrentPosition(showPosition);
}
return false
}