Reputation: 517
I have a LocationService that gets the user location in the background and sends a broadcast to an activity with the Latitude and Longitude. It is the code found in the accepted answer to this question Background service with location listener in android
I created the project with the Google Maps Activity provided by Android Studio. In the MapsActivity I get the broadcast extras like this
public class newMessage extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase(LocationService.BROADCAST_ACTION)) {
Bundle extra = intent.getExtras();
latitude = extra.getDouble("Latitude");
longitude = extra.getDouble("Longitude");
System.out.println("Latitude: "+latitude);
System.out.println("Longitude: "+longitude);
LatLng newLocation = new LatLng(latitude,longitude);
}
}
}
I now want to update the map with the new location but I have no idea how to do this. Is it possible with my current setup?
Upvotes: 0
Views: 478
Reputation: 3582
You need to create a locationService where you can obtain your current location. Check this example:
public class LocationService extends Service implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = LocationService.class.getSimpleName();
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
protected Location mCurrentLocation;
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
buildGoogleApiClient();
mGoogleApiClient.connect();
return START_NOT_STICKY;
}
@Override
public void onConnected(Bundle bundle) {
Log.i("fixedrec", TAG + ">Connected to GoogleApiClient");
if (mCurrentLocation == null) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
}
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onDestroy() {
super.onDestroy();
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
Log.d("fixedrec", TAG+ ">StoppingService");
mNM.cancel(NOTIFICATION);
stopForeground(true);
}
@Override
public void onLocationChanged(Location location) {
//plase where you get your locations
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("fixedrec", TAG + "> Connection failed: ConnectionResult.getErrorCode() = "
+ connectionResult.getErrorCode());
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
Log.i("fixedrec", TAG + "> StartLocationUpdates");
}
protected synchronized void buildGoogleApiClient() {
Log.i("fixedrec", TAG + "> Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
}
To broadcast you location across application you can use BroadCastReceiver or EventBus like Otto. Then just create a googleMap and add a marker with obtained location to it.
Don't forget yo write locationPermissions inside your manifest file and inside your code if you are dealing with SDK >=23
Also you can study this project. Fixedrec3
Everything you need is there.
Upvotes: 1
Reputation: 3763
declare Broadcast receiver in activity and show marker to current location
public BroadcastReceiver locationUpdateReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
//show current location marker
mMap.addMarker(new MarkerOptions().position(/*your lat long*/).title("My Location")));
}
};
Upvotes: 1