Android Open Source - trip-chain-android Route Segment






From Project

Back to project page trip-chain-android.

License

The source code is released under:

MIT License

If you think the Android project trip-chain-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fi.aalto.tripchain.route;
//www  . j  ava2 s . c o  m
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import fi.aalto.tripchain.route.Trip.ActivityModel;

import android.location.Location;

/**
 * Represents a subpath of a trip as the mode of activity and coordinates.
 */
public class RouteSegment {
  final public Activity activity;
  List<Location> locations;
  final private long time;
  
  public RouteSegment(ActivityModel activityModel) {
    this.activity = activityModel.activity;
    this.time = activityModel.timestamp;
    this.locations = new ArrayList<Location>();
  }
  
  public void addLocation(Location location) {
    locations.add(location);
  }
  
  public Location getLastLocation() {
    return this.locations.get(this.locations.size() - 1);
  }
  
  public JSONObject toJson() throws JSONException {
    if (locations.size() == 0) {
      return null;
    }
    
    JSONObject feature = new JSONObject();
    JSONObject geometry = new JSONObject();
    
    JSONArray coordinates = new JSONArray();
    if (locations.size() > 1) {
      geometry.put("type", "LineString");
      
      for (Location l : locations) {
        JSONArray tuple = new JSONArray();
        tuple.put(l.getLongitude());
        tuple.put(l.getLatitude());
        coordinates.put(tuple);
      }

    } else {
      geometry.put("type", "Point");      
      
      coordinates.put(locations.get(0).getLongitude());
      coordinates.put(locations.get(0).getLatitude());
    }
    
    geometry.put("coordinates", coordinates);
    
    JSONObject properties = new JSONObject();
    properties.put("activity", activity.toString());
    properties.put("time", time);
    
    feature.put("geometry", geometry);
    feature.put("properties", properties);
    feature.put("type", "Feature");
    
    return feature;
  }
  
  
}




Java Source Code List

fi.aalto.tripchain.BackgroundService.java
fi.aalto.tripchain.Configuration.java
fi.aalto.tripchain.LoginActivity.java
fi.aalto.tripchain.MainActivity.java
fi.aalto.tripchain.StartFragment.java
fi.aalto.tripchain.TripFragment.java
fi.aalto.tripchain.receivers.ActivityReceiver.java
fi.aalto.tripchain.receivers.EventDispatcher.java
fi.aalto.tripchain.receivers.EventListener.java
fi.aalto.tripchain.receivers.LocationReceiver.java
fi.aalto.tripchain.receivers.Receiver.java
fi.aalto.tripchain.route.ActivityListener.java
fi.aalto.tripchain.route.Activity.java
fi.aalto.tripchain.route.LocationListener.java
fi.aalto.tripchain.route.RoadSegment.java
fi.aalto.tripchain.route.Roads.java
fi.aalto.tripchain.route.RouteSegment.java
fi.aalto.tripchain.route.Route.java
fi.aalto.tripchain.route.TripRecorder.java
fi.aalto.tripchain.route.Trip.java