NativeScript / nativescript-geolocation

Geolocation plugin to use for getting current location, monitor movement, etc
Apache License 2.0
139 stars 76 forks source link

Methods watchLocation / getCurrentLocation hangs #248

Open andresilva-cc opened 4 years ago

andresilva-cc commented 4 years ago

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

Please, tell us how to recreate the issue in as much detail as possible.

I'm using Google Maps SDK and Geolocation, so when mapReady event is triggered, I call a method named startTracking which enable and checks for location permissions, and then calls a method named watchLocation, which will use watchLocation method from nativescript-geolocation. watchLocation does return the watch ID, but it hangs, nothing happens, it does not enter in success callback nor in error callback. I tried before with getCurrentLocation using async/await and the same thing happens, nothing happens and it does not execute the code after this method call.

Is there any code involved?

mapReady:

    async mapReady (args) {
      try {
        // Get map view
        this.mapView = args.object

        // Set map settings
        this.mapView.settings.mapToolbarEnabled = false
        this.mapView.setStyle([
          {
            featureType: 'poi',
            stylers: [{ visibility: 'off' }]
          }
        ])

        // Start tracking
        await this.startTracking()

      } catch (ex) {
        console.log(ex)
        alert(ErrorFormatter(ex))
      }
    },

startTracking:

    async startTracking () {
      try {
        // Ask for location permission
        await geolocation.enableLocationRequest(true, true)

        // Check if location is enabled
        const isEnabled = await geolocation.isEnabled()

        // If location is enabled
        if (isEnabled) {

          // Start location watch
          this.watchLocation()
        }

      } catch (ex) {
        console.log(ex)
        alert(ErrorFormatter(ex))
      }
    },

watchLocation:

    watchLocation () {
      try {
        this.watchId = geolocation.watchLocation(
          loc => {
            if (loc) {
              this.map.latitude = loc.latitude
              this.map.longitude = loc.longitude
            }
          },
          e => {
            console.log(e)
            alert(ErrorFormatter(e))
          },
          {
            desiredAccuracy: Accuracy.HIGH,
            updateDistance: 1,
            updateTime: 3000,
            minimumUpdateTime: 100
          }
        )

      } catch (ex) {
        console.log(ex)
        alert(ErrorFormatter(ex))
      }
    },
andresilva-cc commented 4 years ago

After a while I got: [Error: Timeout while searching for location!]

tbozhikov commented 4 years ago

Hi @DeehSlash, the timeout error you have comes from the getCurrentLocation() API of the plugin rather than watchLocation(). So, could this timeout be unrelated to the code (using watchLocation()) you have pasted in your initial post?

Having the provided info, to me it looks that a hang in your app could be caused by some code like:

await geolocation.watchLocation(...)

Such a call would hang the app either until the location is acquired, or until the timeout is reached. Refer to the code in Android implementation. So, if you use it this way, maybe you should reconsider that.

If that is not your case, a sample app reproducing the issue would be best to be able to assist you further.

Hope this helps.