Hey guys! I'm creating an Android application that returns the current location
of the user. I'm using an IntentService and GreenRobot's 'EventBus' for this.
However, when I post the LatLng back to MainActivity, the value is always null.
How can I access the 'latLng' variable? Moreover, since the onMapReady needs to
be called first, how do I explicitly call it?
import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
//import meetup.be2015.gcm_meetup.MainActivity.MyLocationReceiver;
public class MapClass extends IntentService implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;
private GoogleMap mgoogleMap;
public LatLng latLng;
private GoogleApiClient client;
public MapClass() {
super("MapClass");
}
@Override
public void onCreate(){
super.onCreate();
}
@Override
protected void onHandleIntent(Intent intent) {
//HOW DO I START THE MAP?
}
@Override
public void onMapReady(GoogleMap googleMap) {
Log.d("ON_MAP_READY","In OnMapReady");
mgoogleMap = googleMap;
mgoogleMap.setMyLocationEnabled(true); //Sets location to current
position
buildGoogleApiClient();
mGoogleApiClient.connect();
}
protected synchronized void buildGoogleApiClient() {
Log.d("BUILD", "In buildClient()");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
Log.d("ON_CONNECTED","In onConnected()");
Location MLastLocation =
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (MLastLocation != null) {
latLng = new LatLng(MLastLocation.getLatitude(),
MLastLocation.getLongitude());
//Post the LatLng to MainActivity
EventBus.getDefault().post(new SendLocations(latLng));
//Send sticky event to Register and MyGcmListenerService
//EventBus.getDefault().postSticky(new SendLocations(latLng));
} else {
Log.d("onConnected", "Value of LatLng is NULL");
latLng = new LatLng(0, 0); // <- SENDING ME A NULL LATLNG
EventBus.getDefault().post(new SendLocations(latLng));
}
}
@Override
public void onConnectionSuspended(int i) {
//Notify
Log.d("ConnectionSuspended", "Connection Suspended. Status: " + i);
mgoogleMap.clear();
mGoogleApiClient.disconnect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
//Notify
Log.d("ConnectionFailed", "Connection Failed. Status: " +
connectionResult.toString());
mgoogleMap.clear();
mGoogleApiClient.disconnect();
}
}
Can anyone tell me how to get the proper LatLng and send it back to
MainActivity. Since I added a couple of Log.d statements in each method, I can
actually trace my code. Here's my log:
*03-11 14:54:45.980 13201-13368/meetup.be2015.gcm_meetup D/BUILD: In
buildClient()03-11 14:54:46.670 13201-13201/meetup.be2015.gcm_meetup
D/ON_CONNECTED: In onConnected()03-11 14:54:46.930
13201-13201/meetup.be2015.gcm_meetup D/onConnected: Value of LatLng is NULL*
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/55fb8ff5-7ae5-433d-909d-70e5e4bc9485%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.