2

Related support question: ERROR(1): User denied Geolocation

Please add allow-same-origin to the Stack Snippet sandbox in order to be able to use the Geolocation API.

As example, the following code was taken from the above link. Once I clicked the Show my location button, the result panel shows "Unable to retrieve your location"

function geoFindMe() {
  var output = document.getElementById("out");

  if (!navigator.geolocation){
    output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
    return;
  }

  function success(position) {
    var latitude  = position.coords.latitude;
    var longitude = position.coords.longitude;

    output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';

    var img = new Image();
    img.src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=false";

    output.appendChild(img);
  }

  function error() {
    output.innerHTML = "Unable to retrieve your location";
  }

  output.innerHTML = "<p>Locating…</p>";

  navigator.geolocation.getCurrentPosition(success, error);
}
<p><button onclick="geoFindMe()">Show my location</button></p>
<div id="out"></div>

The referred page includes a demo and buttons to open the code on CODEPEN and on JSFIDDLE where after clicking the "Show my location" button a request for permission is shown and after allowing it an embedded Google Map shows it.

4
  • 1
    But wouldn't that also allow to run the stack snippets in the context of my page and with that execute stuff like voting or worse? I think why this is in effect and shouldn't be changed is explained here by Haney.
    – rene
    Jun 25, 2017 at 18:41
  • @rene: I don't understand what you mean by "the context of my page"? How does this could be done? AFAIK Stack Snippets can only be inserted on Questions and Answers from certain sites on the SE network.
    – Rubén
    Jun 27, 2017 at 15:03
  • I believe, but not 100% sure, that a clever script can also interact with the page on StackOverflow, possible reading my profile info, or voting on my behalf. That is the potential security issue I see with allowing this.
    – rene
    Jun 28, 2017 at 9:20
  • I can't tell if that is or isn't possible but I'm wondering why the code in the question can work on Codepen and JSFiddle but not on Stack Snippet.
    – Rubén
    Jun 28, 2017 at 16:15

0

You must log in to answer this question.

Browse other questions tagged .