Android Open Source - grtransit Route Overlay






From Project

Back to project page grtransit.

License

The source code is released under:

GNU General Public License

If you think the Android project grtransit 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

/*
 * Copyright 2011 Giles Malet./*from ww  w  .  j  av a 2  s  . c  o m*/
 *
 * This file is part of GRTransit.
 * 
 * GRTransit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GRTransit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GRTransit.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.kw.shrdlu.grtgtfs;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

import java.util.zip.Adler32;

public class RouteOverlay extends Overlay {
  private static final String TAG = "BusrouteOverlay";

  private int mCount;
  private int[] mPoints = null;
  private final int mColourDiff;
  private Rect mBoundingBox;

  public RouteOverlay(Context context, String route, String headsign) {
    super();
    // Log.v(TAG, "starting RouteOverlay");

    // Try get different colours for different routes
    final Adler32 chksum = new Adler32();
    chksum.update((route + headsign).getBytes());
    mColourDiff = chksum.hashCode() % 128;

    final String table = "shapes";
    final String[] columns = { "shape_pt_lat", "shape_pt_lon" };
    final String whereclause = "shape_id = (select shape_id from trips where route_id = ? and trip_headsign = ?)";
    final String[] selectargs = { route, headsign };
    final String orderby = "cast(shape_pt_sequence as integer)";

    Cursor csr;
    try {
      csr = DatabaseHelper.ReadableDB().query(table, columns, whereclause, selectargs, null, null, orderby);
    } catch (final SQLException e) {
      Log.e(TAG, "DB query failed: " + e.getMessage());
      return;
    }

    csr.moveToPosition(0);
    mCount = csr.getCount();
    mPoints = new int[mCount * 2];

    // Going to track the edges
    Rect boundingbox = null;

    for (int i = 0; i < mCount; i++) {
      final int stop_lat = (int) (csr.getFloat(0) * 1000000); // microdegrees
      final int stop_lon = (int) (csr.getFloat(1) * 1000000);

      mPoints[i * 2] = stop_lat;
      mPoints[(i * 2) + 1] = stop_lon;

      if (boundingbox == null)
        boundingbox = new Rect(stop_lat, stop_lon, stop_lat, stop_lon);
      else
        boundingbox.union(stop_lat, stop_lon);

      csr.moveToNext();
    }
    csr.close();

    // Stash values needed for later calls
    mBoundingBox = boundingbox;

    // Log.v(TAG, "ending RouteOverlay");
  }

  // Seeing we don't store all points in the overlay, we need to provide our own
  // span values, since the overlay has no clue of what we're doing.
  public Rect getBoundingBoxE6() {
    return mBoundingBox;
  }

  @Override
  public void draw(Canvas canvas, MapView view, boolean shadow) {
    super.draw(canvas, view, shadow);
    // Log.v(TAG, "draw " + shadow);

    if (shadow || mPoints == null || mCount <= 0) return;

    // Convert geo points to points on the canvas
    final Projection proj = view.getProjection();
    final Point pt_scr = new Point();
    final Path path = new Path();

    proj.toPixels(new GeoPoint(mPoints[0], mPoints[1]), pt_scr);
    path.moveTo(pt_scr.x, pt_scr.y);

    for (int i = 1; i < mCount; i++) {
      proj.toPixels(new GeoPoint(mPoints[i * 2], mPoints[(i * 2) + 1]), pt_scr);
      path.lineTo(pt_scr.x, pt_scr.y);
    }

    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // paint.setARGB(128, 224, 64, 32);
    paint.setARGB(255, 96 + mColourDiff, 128 - mColourDiff / 4, 128 + mColourDiff / 2);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(5);
    canvas.drawPath(path, paint);

    // Log.v(TAG, "draw exit");
  }
}




Java Source Code List

net.kw.shrdlu.grtgtfs.DatabaseHelper.java
net.kw.shrdlu.grtgtfs.GRTApplication.java
net.kw.shrdlu.grtgtfs.NavOptions.java
net.kw.shrdlu.grtgtfs.NotificationCallback.java
net.kw.shrdlu.grtgtfs.Preferences.java
net.kw.shrdlu.grtgtfs.RouteOverlay.java
net.kw.shrdlu.grtgtfs.ServiceCalendar.java
net.kw.shrdlu.grtgtfs.StopsOverlay.java
net.kw.shrdlu.grtgtfs.Activities.ClosestStopsActivity.java
net.kw.shrdlu.grtgtfs.Activities.FavstopsActivity.java
net.kw.shrdlu.grtgtfs.Activities.MenuListActivity.java
net.kw.shrdlu.grtgtfs.Activities.MenuMapActivity.java
net.kw.shrdlu.grtgtfs.Activities.PrefsActivity.java
net.kw.shrdlu.grtgtfs.Activities.RiderAlertsActivity.java
net.kw.shrdlu.grtgtfs.Activities.RouteActivity.java
net.kw.shrdlu.grtgtfs.Activities.RouteselectActivity.java
net.kw.shrdlu.grtgtfs.Activities.SearchActivity.java
net.kw.shrdlu.grtgtfs.Activities.StartupActivity.java
net.kw.shrdlu.grtgtfs.Activities.StopsActivity.java
net.kw.shrdlu.grtgtfs.Activities.TimesActivity.java
net.kw.shrdlu.grtgtfs.Activities.TripStopsActivity.java
net.kw.shrdlu.grtgtfs.LayoutAdapters.FavstopsArrayAdapter.java
net.kw.shrdlu.grtgtfs.LayoutAdapters.ListArrayAdapter.java
net.kw.shrdlu.grtgtfs.LayoutAdapters.ListCursorAdapter.java
net.kw.shrdlu.grtgtfs.LayoutAdapters.NavDrawerItem.java
net.kw.shrdlu.grtgtfs.LayoutAdapters.NavDrawerListAdapter.java
net.kw.shrdlu.grtgtfs.LayoutAdapters.TimeStopdescArrayAdapter.java
net.kw.shrdlu.grtgtfs.LayoutAdapters.TimesArrayAdapter.java