In my mobile application I am using OpenLayers and trying to capture current position of the user by using GeoLocation.getPosition() method.

The method sometimes returns position and sometimes return undefined. The behaviour is strange ! The GeoLocation object has value in it. Below is my code:

Geolocation object creation :

this.GeoLocationObj = new GeoLocation({
        projection: this.map.getView().getProjection(),
        tracking: true,
        trackingOptions: {
            enableHighAccuracy: true

Use of this to get the position:

  var position = this.GeoLocationObj.getPosition();
  this.positionFeature.setGeometry(new Point(position) )

What should I change?

  • 1
    Undefined means the device hasn't located itself or there has been an error. As in the example openlayers.org/en/latest/examples/geolocation.html you should use this.GeoLocationObj.on('error', ...) as well as this.GeoLocationObj.on('change:position', ...) – Mike Oct 20 '19 at 11:36
  • Thanks for the response , I believe device did not locate itself. I waited for few mins after enabling GPS in my phone and then the app worked fine. – user684805 Oct 22 '19 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.