Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_locationService.getLocation() not returning anything on iPad. #361

Closed
Shivamtruein opened this issue May 16, 2020 · 22 comments
Closed

_locationService.getLocation() not returning anything on iPad. #361

Shivamtruein opened this issue May 16, 2020 · 22 comments
Labels

Comments

@Shivamtruein
Copy link

Shivamtruein commented May 16, 2020

Describe the bug
The _locationService.getLocation() is not returning anything on Ipad mini wifi model.
The code gets to this line and after that, it never returns anything.

Expected behavior
_locationService.getLocation() should return location.

Tested on:

  • Android - On Android, it's working fine.
  • iOS, Ipad mini 2018 wifi only model it's not working.

Additional logs

Code written

try { _locationService.changeSettings( accuracy: LocationAccuracy.HIGH, ); currentLocation = await _locationService.getLocation(); return true; } catch (e) { print(e); if (e.code == 'PERMISSION_DENIED') { _locationService.requestPermission(); } else if (e.code == 'SERVICE_STATUS_DISABLED') { _locationService.requestService(); } currentLocation = null; return false; }

@ZantsuRocks
Copy link

The localization service was enabled when requesting the localization?

@Shivamtruein
Copy link
Author

Yes, the service was enabled when I was trying. I checked before hitting the code, even tried manually on and off the service but no result.

@YarMag
Copy link

YarMag commented May 28, 2020

Same problem. await _locationService.getLocation() never returns on the all iOS simulators. That happened after I've updated my Xcode. I suppose, it could be somehow related to updated iOS version on virtual devices.

@ZantsuRocks
Copy link

I opened a pull request with the fix.
Waiting for approval.

PR: #362

@Shivamtruein
Copy link
Author

@ZantsuRocks When can we expect the resolution?

@daniloapr
Copy link

Same problem here for iPhone devices. I am doing a work around using the onLocationChanged

@rasmusir
Copy link

Make sure you set the device location in xcode, if you don't getLocation() will never return.

@Hyla96
Copy link

Hyla96 commented Jul 6, 2020

Same error here, running targetting iOS 10.0. For me does not work onLocationChanged aswell, tried a workaround with

LocationData loc = await Location().onLocationChanged.first

But it still does not return anything

#Edit

I did manager to solve it:

The problem was that I called a listener to listen changes of position and than I tried to call again .getLocation(), basically the sequence of events was:

-> Instantiate Location and get once .getLocation() -> Returned the current location;
-> Instantieted a listener to listen changes of user's position;
-> Called again .getLocation() -> Did not return anything, just kept going.

To solve it I put a provider over my flutter application, and passed trough context my LocationHelper, where I stored lastPositionKnown, so now:

-> Instantiate Location, get once .getLocation() and save it in lastPositionKnown;
-> Instantiate a position listener and each change is saved in lastPositionKnown;
-> When I need again current position I call a custom getLocation that returns lastPositionKnown;

I don't know if this library has a lastPositionKnown, but It wouldn't be that bad to have it.

@dave-k
Copy link

dave-k commented Jul 13, 2020

@Hyla96 I have the same issue. The first time .getLocation() returns the current location but the second time it does not return anything. Would you be able to share the code for your solution?

@Hyla96
Copy link

Hyla96 commented Jul 13, 2020

@dave-k

At the moment I'm not able to, but you can follow the following steps to workaround it:

Create a LocationHelper class where you fetch and save location into a variable:

class LocationHelper{

 Location _locationdata;
 
LocationHelper(){
  this._locationdata = fetchLocation();
}
Location getLocation(){
 return _locationdata;
}
Location fetchLocation(){
 //Here you call once the location
}
}

Now you place a Provider over your main app:

Provider(
  create: (_) => LocationHelper(),
  child: MyMainApp()
)

Now when you need your location instead calling it again you can call

Provider.of<LocationHelper>(context).getLocation();

I'm sorry but I've written it from my phone, just follow the logic and implement the code properly. You can also add a listener in your locationhelper in order to update the locationdata in case it changes. This worked great for me :)

@dave-k
Copy link

dave-k commented Jul 14, 2020

@Hyla96

Location _locationdata;
I was confused by the above statement. Is _locationdata of type Location or LocationData?

Here is my code.

There is one issue. Provider.of<LocationData>(context, listen: false); returns null the first time it is called.

LocationHelper.dart:

class LocationHelper {
  LocationHelper(){
    _locationdata = fetchLocation();
  }
  Future<LocationData> _locationdata;

  Future<LocationData> getLocation() {
    return _locationdata;
  }
  Future<LocationData> fetchLocation() async {
    // Call Location() once
    return await Location().getLocation();
  }
}

main.dart

FutureProvider<LocationData>(create: (_) => LocationHelper().getLocation())

locationscreen.dart

final LocationData _locationData = Provider.of<LocationData>(context, listen: false);
if(_locationData != null) {
  print('latitude: ${_locationData.latitude} longitude: ${_locationData.longitude}');
}

@Hyla96
Copy link

Hyla96 commented Jul 14, 2020

Yeah I'm sorry, that should be a LocationData. However, you should modify that provider and turn it into a LocationHelper() and get the location in a initialization method, you are actually passing LocationData with that provider, and sound really awkward to me, because that's a future and weird things might happen. So:

  1. In the provider pass the entire class LocationHelper and not just LocationData;
  2. Call fetch location after you have initialized LocationHelper (await Provider.of(context, listen:false).fetchLocation()) somewhere in a init() method;
  3. Make _locationdata and _getlocation synchronous, that Future doesn't really make sense tbh

@dave-k
Copy link

dave-k commented Jul 14, 2020

@Hyla96
I followed your explanation and this code seems to be working.

LocationHelper.dart:

class LocationHelper{
  LocationData _locationData;
  
  LocationData getLocation(){
    return _locationData;
  }

  LocationData fetchLocation(){
    Location().getLocation()
      .then((LocationData locationData) => _locationData = locationData);
  }
}

main.dart

Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        Provider<LocationHelper>(create: (_) => LocationHelper())
      ],
      child: MaterialApp(

. . .

@override
  Widget build(BuildContext context) {
    Provider.of<LocationHelper>(context, listen:false).fetchLocation();

locationscreen.dart

    final LocationData _locationData = Provider.of<LocationHelper>(context, listen:false).getLocation();
    print('latitude: ${_locationData.latitude} longitude: ${_locationData.longitude}');

@bhaveshbusa
Copy link

Make sure you set the device location in xcode, if you don't getLocation() will never return.

What should the behaviour be when device location in xcode simulator is None? getLocation code hangs of me

Is there a way to check location is None to avoid being blocked on the getLocation call?

@stale
Copy link

stale bot commented Aug 22, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the inactive label Aug 22, 2020
@stale stale bot closed this as completed Aug 29, 2020
@aacassandra
Copy link

still not working on ios.

@acamue
Copy link

acamue commented Nov 29, 2021

Still not working on ios here !!!!!how to solve ???

@RogerBrusamarello
Copy link

Any updates? It still not working on iOS. Apple is rejecting my app because of this.

@LaysDragon
Copy link

I feeling that the gps feature on ios simulator is soooo broke compared to android simulator...it don't return the result while using custom location, and don't even have map to pick up location like android...

@alidev0
Copy link

alidev0 commented Jan 24, 2023

Still an issue

@alidev0
Copy link

alidev0 commented Jan 24, 2023

Same error here, running targetting iOS 10.0. For me does not work onLocationChanged aswell, tried a workaround with

LocationData loc = await Location().onLocationChanged.first

But it still does not return anything

#Edit

I did manager to solve it:

The problem was that I called a listener to listen changes of position and than I tried to call again .getLocation(), basically the sequence of events was:

-> Instantiate Location and get once .getLocation() -> Returned the current location; -> Instantieted a listener to listen changes of user's position; -> Called again .getLocation() -> Did not return anything, just kept going.

To solve it I put a provider over my flutter application, and passed trough context my LocationHelper, where I stored lastPositionKnown, so now:

-> Instantiate Location, get once .getLocation() and save it in lastPositionKnown; -> Instantiate a position listener and each change is saved in lastPositionKnown; -> When I need again current position I call a custom getLocation that returns lastPositionKnown;

I don't know if this library has a lastPositionKnown, but It wouldn't be that bad to have it.

nailed it. this was my case. just removed the gps data stream and works.

@vanlooverenkoen
Copy link

vanlooverenkoen commented Feb 9, 2023

Still not working as it should.
This is our current implementation/workaround.

setInitialLocation is used in the splash
onLocationChanged is used on the map
getLocation is used to "trick this package"

  @override
  Stream<LocationTrackingData> onLocationChanged() {
    if (Platform.isIOS) {
      getLocation();
    }
    return location.onLocationChanged.map((location) => LocationTrackingData(
          latitude: location.latitude,
          longitude: location.longitude,
          accuracy: location.accuracy,
          isMock: location.isMock ?? false,
        ));
  }

  @override
  Future<LocationTrackingData> getLocation() async {
    final location = await this.location.getLocation();
    return LocationTrackingData(
      latitude: location.latitude,
      longitude: location.longitude,
      accuracy: location.accuracy,
      isMock: location.isMock ?? false,
    );
  }

  @override
  Future<void> setInitialLocation() async {
    final status = await this.location.hasPermission();
    if (status != PermissionStatus.granted) return;
    final location = await onLocationChanged().first;
    final lat = location.latitude;
    final lon = location.longitude;
    if (lat == null || lon == null) return;
    _initialLocation = LatLonData(latitude: lat, longitude: lon);
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests