13,097
社区成员




try {
Criteria criteria = new Criteria();
criteria.setSpeedAndCourseRequired(true);
LocationProvider lp = LocationProvider.getInstance(criteria);
if (lp != null) {
Location l = lp.getLocation(300);
if (l.isValid()) {
Coordinates c = l.getQualifiedCoordinates();
if (c != null) {
float course = l.getCourse();
float speed = l.getSpeed() * 3.6f;
double latitude = c.getLatitude();
double longitude = c.getLongitude();
String latitudeString = "Latitude: " + latitude;
String longitudeString = "Longitude: " + longitude;
String courseString = "Course: " + course + '°';
String speedString = "Speed: " + speed;
if (speedString.length() > 12)
speedString = speedString.substring(0, 12);
speedString += "km/h";
if (start)
System.out.println(latitudeString + "\n"
+ longitudeString + "\n" + courseString
+ "\n" + speedString);
}
} else {
System.out.println("Location is not valid!");
}
} else {
System.out.println("LocationProvider = null!");
}
} catch (LocationException le) { // not able to retrieve location
// information
System.out.println("LocationException: " + le.getMessage());
} catch (InterruptedException ie) {
System.out.println("InterruptedException: " + ie.getMessage());
}