13,100
社区成员
发帖
与我相关
我的任务
分享
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) {
course = l.getCourse(); // moving direction is degrees
speed = l.getSpeed() * 3.6f; // m/s converted to km/h
double latitude = c.getLatitude();
double longitude = c.getLongitude();
latitudeString = "Latitude: " + latitude;
longitudeString = "Longitude: " + longitude;
courseString = "Course: " + course + '°';
speedString = "Speed: " + speed;
if (speedString.length() > 12) speedString = speedString.substring(0, 12);
speedString += "km/h";
}
}
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());
}