I recently learned about the JavaScript Geolocation API, and I want to view the output on my mobile phone while coding on my PC using VS Code and Live Server.
here is my code
const display = document.getElementById("display");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
display.innerHTML = "Geolocation is not available in your browser!";
}
}
function showPosition(position) {
display.innerHTML = "Latitude: " + position.coords.latitude + "<br/> Longitude: " + position.coords.longitude;
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
display.innerHTML = "User denied the request for Geolocation.";
break;
case error.POSITION_UNAVAILABLE:
display.innerHTML = "Location information is unavailable.";
break;
case error.TIMEOUT:
display.innerHTML = "The request to get user location timed out.";
break;
case error.UNKNOWN_ERROR:
display.innerHTML = "An unknown error occurred.";
break;
}
}
How can I correctly access localhost from multiple devices on the same Wi-Fi network?
Are there any additional configurations or settings I need to adjust to make this work?
I attempted to access the localhost URL from my PC’s browser and then copied and pasted it into my mobile phone’s browser. However, this didn’t work.
Pritam Ghosh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.