2

I'm working in Google Earth Engine and I want to access the user's location to then be able to use the position when visualizing data layers. When running the code below, GEE doesn't seem to be able to understand the navigator which contains the geolocation info I'm looking for. Same goes for document. This makes me wonder whether GEE even works in JS since it doesn't understand the queries(?) Anyone with a solution to access the user location?

var locationButton = ui.Button('User location');
locationButton.onClick(function() {

  var startPos;
  var geoSuccess = function(position) {
    startPos = position;
    document.getElementById('startLat').innerHTML = startPos.coords.latitude;
    document.getElementById('startLon').innerHTML = startPos.coords.longitude;
     if (navigator.geolocation) {
      console.log('Geolocation is supported!');
    }
    else {
      console.log('Geolocation is not supported for this Browser/OS.');
    }
  };
  navigator.geolocation.getCurrentPosition(geoSuccess);

});
Map.add(locationButton);
2
  • as per the answer to this question gis.stackexchange.com/questions/307963/… GEE doesn't run in your browser's JavaScript, but in a sandboxed JS system. I asked and I didn't get any answer as to what are the specifications of this JS system and whether it complies to any standard. The conclusion is that some JS features are just disabled and that's that. I can't put this as an answer as I'm not sure yet that it's true, but I am afraid the answer for you is that it's just not possible currently.
    – Andrei
    Commented Mar 10, 2019 at 15:23
  • 1
    Thank you @Andrei for your clarification. I solved the issue by letting the user choose their location of interest by clicking in the map using onClick Commented Mar 12, 2019 at 16:18

1 Answer 1

1

You may use

ui.util.getCurrentPosition(success, error, options)

Here's a simple example:

function current_position(point) {
  Map.addLayer(point);
  Map.centerObject(point);
  print(point);
}

function oops(error) {
  print(error);
}

ui.util.getCurrentPosition(current_position, oops);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.