So i'm using google cloud messaging to send coordinates from one phone to another. This coordnates are saved in sqlite and when i open the MapActivity i can draw a polyline on the map using this coordinates.
However if the MapActivity is activ and i receive new coordinates from google cloud messaging i want it to update the polyline on the map without needing too restart it.
BroadcastReceiver to retrive coordinates
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
IntentService to handle the message and get the coordinates from here i want to send the new coordinates to MapActivity to update the polyline
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
//...
//extract the coordinates from the message and saves it in sqlite
// from here i want to call MapsActivity and update the polyline
//...
}
And my MapActivity
public class MapsActivity extends FragmentActivity implements LocationListener {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private MarkerOptions theMarker = new MarkerOptions();
public static int userId = 0;
public static PolylineOptions polylineOptions = null;
public static Polyline polyline = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Intent intent = getIntent();
userId = intent.getIntExtra("userId",0);
setUpMapIfNeeded();
drawLine();
}
public static void addLine(double lat, double lng){
LatLng coord = new LatLng(lat,lng);
Log.d("adding..","adding..");
polylineOptions.add(coord);
}
private void drawLine() {
polylineOptions = new PolylineOptions()
.width(10)
.color(Color.RED);
DataSource dataSource = new DataSource(this);
dataSource.open();
List<CoordinatesObject> coordinatesObjects = dataSource.getCoordinatesForUser(userId);
dataSource.close();
for(CoordinatesObject co : coordinatesObjects){
android.util.Log.d("Lat",String.valueOf(co.getLat()));
android.util.Log.d("Lng",String.valueOf(co.getLng()));
polylineOptions.add(new LatLng(co.getLat(), co.getLng()));
}
android.util.Log.d("userId",String.valueOf(userId));
polyline = mMap.addPolyline(polylineOptions);
}