接收新坐标时添加折线

Add polyline when receiving new coordinates

所以我正在使用 google 云消息将坐标从一个 phone 发送到另一个。这个坐标保存在 sqlite 中,当我打开 MapActivity 时,我可以使用这个坐标在地图上绘制一条折线。

但是,如果 MapActivity 处于活动状态并且我从 google 云消息接收到新坐标,我希望它更新地图上的多段线而无需重新启动它。

用于检索坐标的 BroadcastReceiver

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 处理消息并从这里获取坐标我想将新坐标发送到 MapActivity 以更新折线

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
    //...
}

还有我的 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);
}

您必须检查 GcmIntentService Is MapActivity is open or not?。如果它是打开的,则向 MapActivity 发送一个广播。否则向 MapActivity 发送一个意图。