コード例 #1
0
  public Location getLocation(int timeout) throws LocationException, InterruptedException {
    /* TODO The timeout might play to the fact that it is still acquiring satellites?
     * I was wondering about that before. Maybe it makes sense to have timeout in SimpleGPS?
     * TODO: Solution! Keep asking for altitude until is positive? (longitude can be negative)
     * Or perhaps just until speed positive? (set those after)
     * TODO: -1 in timeout is supposed to represent the default timeout (GPSListener?)
     * TODO: I don't know if this is supposed to wait for the GPS to provide a new
     * coordinate data or if it is okay to pass the latest cached GPS coordinates.
     * Is the purpose of the timeout that it gets a new updated location that
     * is not the previously returned or cached one?
     */

    if (timeout == 0) throw new IllegalArgumentException("timeout cannot equal 0");

    // Timeout results in LocationException:
    long startTime = System.currentTimeMillis();

    // TODO: Perhaps initialize and test for NaN instead.
    while (gps.getLatitude() == 0 & gps.getLongitude() == 0) {
      if (timeout != -1 & System.currentTimeMillis() - startTime > (timeout * 1000))
        throw new LocationException("GPS timed out");
      Thread.sleep(100); /* NOTE: This might very occasionally cause an error because
			* Thread.yield() seems to cause sentence parsing to start too soon.
			* (try changing sleep() to yield() to see what happens)
			* Perhaps something needs to be synchronized? */
    }

    QualifiedCoordinates qc =
        new QualifiedCoordinates(
            gps.getLatitude(),
            gps.getLongitude(),
            gps.getAltitude(),
            (gps.getHDOP() * 6),
            (gps.getVDOP() * 6));
    Location loc =
        new Location(
            qc,
            gps.getSpeed(),
            gps.getCourse(),
            gps.getTimeStamp(),
            0,
            null); // TODO: Implement location method and extraInfo (0 and null for now)

    return loc;
  }