Html 5 Geo Location Prompt in Chrome

HTML 5 Geo Location Prompt in Chrome

There's some sort of security restriction in place in Chrome for using geolocation from a file:/// URI, though unfortunately it doesn't seem to record any errors to indicate that. It will work from a local web server. If you have python installed try opening a command prompt in the directory where your test files are and issuing the command:

python -m SimpleHTTPServer

It should start up a web server on port 8000 (might be something else, but it'll tell you in the console what port it's listening on), then browse to http://localhost:8000/mytestpage.html

If you don't have python there are equivalent modules in Ruby, or Visual Web Developer Express comes with a built in local web server.

Geolocation prompt dialog in Chrome, how do I implement that?

but getCurrentPosition requires at least 1 argument and I can't seem to get it working.

So you need an example of how to pass a suitable argument.

All the JavaScript code that I've found online worked just fine, but all of them were writing in the console Your latitude is: and Your longitude is: but I don't need that.

So you have examples of how to pass an argument. That argument being a function which does something with the position. You just don't like what it does with the position.

Use that example.

Edit the function so that the lines which log the information do whatever you want to do with the data instead.

Here is the version from MDN:

var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};

function success(pos) {
var crd = pos.coords;

console.log('Your current position is:');
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
console.log(`More or less ${crd.accuracy} meters.`);
};

function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
};

navigator.geolocation.getCurrentPosition(success, error, options);

W3C Geolocation API not working in Chrome

Works perfectly for me - with both Chrome 11 and Firefox 4.0.1 on Win 7

  • Make sure you've not disabled location tracking in Chrome: Options > Under the Hood > Content Settings > Location
  • Because of security restrictions, resources loaded with the file:/// scheme are not allowed access to location. See HTML 5 Geo Location Prompt in Chrome.

HTML5 Geolocation not prompting for location on Safari browser

I solved my problem by handling errors. Safari and ios seem to require error handling to work.

This works :

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}

function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}

function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>

</body>
</html>


Related Topics



Leave a reply



Submit