接收新坐标时添加折线

时间:2015-05-14 12:55:06

标签: java android google-maps polyline

所以我使用谷歌云消息传递将坐标从一部手机发送到另一部手机。这个coordnates保存在sqlite中,当我打开MapActivity时,我可以使用这个坐标在地图上绘制折线。

然而,如果MapActivity是激活的并且我从谷歌云消息收到新的坐标,我希望它更新地图上的折线而不需要重新启动它。

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);
}

1 个答案:

答案 0 :(得分:0)

您必须签入GcmIntentService是否已打开MapActivity?如果它是打开的,那么发送广播给MapActivity。否则向MapActivity发出意图。