Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import com.google.transit.realtime.GtfsRealtime;
  2.  
  3. import java.io.IOException;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6.  
  7. /**
  8. * Created by niklasgs on 2015-11-30.
  9. */
  10. public class PublisherTest
  11. {
  12. public static void main(String[] args)
  13. {
  14. /*
  15. Publisher thePublisher = new Publisher("Publisher1", "193.10.227.204", 6237);
  16.  
  17. // Don’t log publications.
  18. thePublisher.setLogWriting(false);
  19.  
  20. // Don’t print publications to screen.
  21. thePublisher.setTesting(false);
  22. thePublisher.connect();
  23.  
  24. // New publication.
  25. HashtablePublication thePublication = new HashtablePublication();
  26. // Publication time.
  27. thePublication.setStartTime(currentTimeMillis());
  28. // Time of validity: 30 seconds.
  29. thePublication.setValidity(currentTimeMillis() + 30000);
  30. // Set buss ID and its coordinates as name-value pairs.
  31. thePublication.setProperty("BusId", "#10");
  32. thePublication.setProperty("Latitude", "42.35");
  33. thePublication.setProperty("Longitude", "-71.12");
  34. // Send publication to MoPS broker.
  35. thePublisher.pub(thePublication);
  36. // Disconnect from the broker.
  37. thePublisher.disconnectFromBroker();
  38. */
  39.  
  40. URL vehUrl;
  41. try {
  42. vehUrl = new URL( "http://developer.mbta.com/lib/gtrtfs/Vehicles.pb" );
  43. System.out.println("Connecting to " + vehUrl);
  44. GtfsRealtime.FeedMessage theFeed = GtfsRealtime.FeedMessage.parseFrom(vehUrl.openStream());
  45. for ( GtfsRealtime.FeedEntity anEntity : theFeed.getEntityList() )
  46. {
  47. if (!anEntity.hasVehicle()) continue;
  48.  
  49. GtfsRealtime.VehiclePosition aVehicle = anEntity.getVehicle();
  50. if (!aVehicle.hasPosition()) continue;
  51.  
  52. GtfsRealtime.Position aPosition = aVehicle.getPosition();
  53. float aLatitude = aPosition.getLatitude();
  54. float aLongitude = aPosition.getLongitude();
  55.  
  56. System.out.println("Position for Vehicle " + anEntity.getId() + " Lat: " + aLatitude + " Long: " + aLongitude);
  57. }
  58. } catch ( MalformedURLException ex ) {
  59. System.out.println("MalformedURLException " + ex.getMessage());
  60. } catch ( IOException ex ) {
  61. System.out.println("IOException " + ex.getMessage());
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement