I want to set up marker on Google Map when GPS is enabled.
I created a broadcast receiver to check if GPS is enabled or disabled. It works. However, i don't know how to get Location and set up marker on map with Google Play services.
Method connect() on a LocationClient is started on method onStart() and startUpdates() is started on onResume().
How can i setup map on my Broadcast receiver ?
If i use getLocation() (see below) it returns null because i'm not connected to GooglePlay services.
If i use LocationClient.connect() i have to wait the client be connected to get location.
How can i do that ?
PS : I use this sample of code to connect to Google play services : http://developer.android.com/training/location/receive-location-updates.html
My inner class GpsLocationReceiver :
public class GpsLocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
LocationManager lm = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE);
boolean isEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
onGpsStatusChanged(isEnabled);
}
}
private void onGpsStatusChanged(boolean b) {
if (!servicesConnected() && b) {
mLocationClient.connect();
}
/*currentLocation = getLocation();
//setUpMapIfNeeded();
if (currentLocation == null) {
Toast.makeText(this, "GPS enabled - " + b + " Loc : null", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "GPS enabled - " + b + " Loc : " + currentLocation.toString(), Toast.LENGTH_LONG).show();
}*/
}
My method getLocation
public Location getLocation() {
// If Google Play Services is available
if (servicesConnected()) {
// Get the current location
return mLocationClient.getLastLocation();
}
return null;
}
my method onConnected() :
@Override
public void onConnected(Bundle bundle) {
//Set currentLocation
currentLocation = getLocation();
if (currentLocation == null) {
Toast.makeText(HomeActivity.this, "location null", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(HomeActivity.this, "Lat : "+ currentLocation.getLatitude() + " Long : "+currentLocation.getLongitude(), Toast.LENGTH_LONG).show();
//Get map if needed
setUpMapIfNeeded();
}
if (mUpdatesRequested) {
startPeriodicUpdates();
}
}
Thx
EDIT : I had modify my code. It seems clearer for me. Now, my function getLocation() called after connect) finished succesfully return null. That means that google play service is not available.
How is it possible because connection to the service is finished successfully ?