0

Im using GoogleMap.OnMyLocationChangeListener to get the current location (below)

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
    @Override
    public void onMyLocationChange(Location location) {
        start = new LatLng(location.getLatitude(), location.getLongitude());
        map.addMarker(new MarkerOptions().position(start));
    }
};

However, I need a way to wait for the listener to complete. Reason being that, Start var will obviously be null if I continue to execute other code without location data. I thought that AsyncTask might be useful here, but have no idea what to call inside doInBackground/onPostExecute.

map.setOnMyLocationChangeListener(myLocationChangeListener); // set the listener
// perform wait and initiate start variable
String url = getDirectionsUrl(start, destination);
// a sync task here using the url

Any hints or code snippets would be much appreciated~!

KaiteN
  • 53
  • 7
  • 1
    What part of your code do you need to wait for? Is it possible to use conditional statements to skip that code until start != null. – zgc7009 May 15 '15 at 17:57
  • I doubt thats an option. I need the location information after setting the listener. => map.setOnMyLocationChangeListener(myLocationChangeListener); String url = getDirectionsUrl(start, destination); – KaiteN May 15 '15 at 18:01
  • 2
    What you could do is set your Listener, create a method that does what you want to do after your have gotten your location, and then in `onMyLocationChange(Location location)` call that method instead of just letting the code run even though you know you won't have the value you need. This way you will pause the functionality until you are certain you have your `start` value without blocking up the UI thread. – zgc7009 May 15 '15 at 18:04
  • 1
    Hmm that works >< beating myself for not figuring this out myself... thank you sir. – KaiteN May 15 '15 at 18:12
  • No problem, I do recommend looking into FusedLocationProvider as that is the recommended method now. You should be able to implement your current fix into a FusedLocationProvider fairly simply, the samples provided by Android are pretty straight forward. Happy coding :) – zgc7009 May 15 '15 at 18:28
  • @KaiteN I just wrote up a detailed answer on how to use FusedLocationProvider in conjunction with a SupportMapFragment, it might help if you decide to go down that path: http://stackoverflow.com/questions/30253123/blue-dot-and-circle-is-not-shown-on-mylocation-using-android-fused-location-api/30255219#30255219 – Daniel Nugent May 15 '15 at 18:40
  • Already moved to Fused as CommonsWare suggested :) thanks Daniel, good stuff! – KaiteN May 15 '15 at 18:43
  • Regarding fused location, I can recommend this library : https://github.com/mcharmas/Android-ReactiveLocation – cYrixmorten May 15 '15 at 18:46

1 Answers1

2

I need the location information after setting the listener

That is not possible. There may not be any "location information after setting the listener". It may take some time to get a location fix. Given your existing code, your first opportunity to have location data will be in onMyLocationChange(), and you cannot use the location data before that point.

Also note that setOnMyLocationChangeListener() has been deprecated for about a year. Quoting the documentation:

This method is deprecated. use FusedLocationProviderApi instead. FusedLocationProviderApi provides improved location finding and power usage and is used by the "My Location" blue dot. See the MyLocationDemoActivity in the sample applications folder for example example code, or the Location Developer Guide.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491