question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getCurrentPosition returns immediately with undefined instead of result from onSuccess argument

See original GitHub issue

Environment

System:
    OS: macOS 12.0.1
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
    Memory: 24.38 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.4 - /usr/local/bin/node
    Yarn: Not Found
    npm: 8.5.3 - /usr/local/bin/npm
    Watchman: 2021.08.30.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /Users/rafael/.rbenv/shims/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 15.4, macOS 12.3, tvOS 15.4, watchOS 8.5
    Android SDK:
      API Levels: 28, 29, 30, 31
      Build Tools: 28.0.3, 29.0.2, 30.0.2, 31.0.0
      System Images: android-29 | Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 2020.3 AI-203.7717.56.2031.7583922
    Xcode: 13.3/13E113 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_292 - /usr/bin/javac
    Python: 3.9.6 - /Users/rafael/.pyenv/shims/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.4 => 0.63.4 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Platforms

Is this issue related to Android, iOS, or both ? Both

Versions

  • Android: 11
  • iOS: 15.4
  • react-native-geolocation-service: ^2.0.2
  • react-native: 0.63.4
  • react: 16.13.1

Description

Instead of returning result from onSuccess param, getCurrentPosition returns immediately with undefined, and callback is called afterwards.

Reproducible Demo

const test = async () => {
    console.log("Starting test");
    const result = await getCurrentPosition(
        (position) => {
            console.log("Got position");
            return true;
        },
    );
    console.log("Finishing test, result:", result);
);

Expected Results

Expected logs:

Starting test
Got position
Finishing test, result: true

Actual logs:

Starting test
Finishing test, result: undefined
Got position

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Agontukcommented, May 27, 2022

Closing this as latest version contains the proper typings.

1reaction
rafabulsingcommented, Apr 22, 2022

Actually, getCurrentPosition is async (returns a Promise) in the version I have (2.0.2). At least that’s what index.d.ts is describes it as. Lines 61-65:

  export function getCurrentPosition(
    successCallback: SuccessCallback,
    errorCallback?: ErrorCallback,
    options?: GeoOptions
  ): Promise<GeoPosition>

In any case, I decided to do a workaround to get it to work as I need it to. Here it is, in case anyone is in the same situation:

const getLocation = (onSuccess, onError, options) => new Promise((resolve, reject) => {
  Geolocation.getCurrentPosition(
    (result) => resolve(onSuccess(result)),
    (error) => reject(onError(error)),
    options,
  );
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

navigator.geolocation.getCurrentPosition sometimes works ...
It does not time the user to allow or deny the geolocation request, but how long the network is allowed to take to...
Read more >
Geolocation.getCurrentPosition() - Web APIs | MDN
A positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return.
Read more >
MediaPlayer - Android Developers
Although the asynchronuous seekTo(long, int) call returns right away, the actual seek operation may take a while to finish, especially for audio/video being ......
Read more >
cordova-plugin-geolocation
navigator.geolocation.getCurrentPosition. Returns the device's current position to the geolocationSuccess callback with a Position object as the parameter.
Read more >
Why does the geolocation API return undefined? - Reddit
The below code returns undefined. Why is that? In my mind the logic would be that the getLocation(), upon a successful retrieval of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found