Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.transit.realtime.GtfsRealtime;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- /**
- * Created by niklasgs on 2015-11-30.
- */
- public class PublisherTest
- {
- public static void main(String[] args)
- {
- /*
- Publisher thePublisher = new Publisher("Publisher1", "193.10.227.204", 6237);
- // Don’t log publications.
- thePublisher.setLogWriting(false);
- // Don’t print publications to screen.
- thePublisher.setTesting(false);
- thePublisher.connect();
- // New publication.
- HashtablePublication thePublication = new HashtablePublication();
- // Publication time.
- thePublication.setStartTime(currentTimeMillis());
- // Time of validity: 30 seconds.
- thePublication.setValidity(currentTimeMillis() + 30000);
- // Set buss ID and its coordinates as name-value pairs.
- thePublication.setProperty("BusId", "#10");
- thePublication.setProperty("Latitude", "42.35");
- thePublication.setProperty("Longitude", "-71.12");
- // Send publication to MoPS broker.
- thePublisher.pub(thePublication);
- // Disconnect from the broker.
- thePublisher.disconnectFromBroker();
- */
- URL vehUrl;
- try {
- vehUrl = new URL( "http://developer.mbta.com/lib/gtrtfs/Vehicles.pb" );
- System.out.println("Connecting to " + vehUrl);
- GtfsRealtime.FeedMessage theFeed = GtfsRealtime.FeedMessage.parseFrom(vehUrl.openStream());
- for ( GtfsRealtime.FeedEntity anEntity : theFeed.getEntityList() )
- {
- if (!anEntity.hasVehicle()) continue;
- GtfsRealtime.VehiclePosition aVehicle = anEntity.getVehicle();
- if (!aVehicle.hasPosition()) continue;
- GtfsRealtime.Position aPosition = aVehicle.getPosition();
- float aLatitude = aPosition.getLatitude();
- float aLongitude = aPosition.getLongitude();
- System.out.println("Position for Vehicle " + anEntity.getId() + " Lat: " + aLatitude + " Long: " + aLongitude);
- }
- } catch ( MalformedURLException ex ) {
- System.out.println("MalformedURLException " + ex.getMessage());
- } catch ( IOException ex ) {
- System.out.println("IOException " + ex.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement