【发布时间】:2012-03-27 10:37:06
【问题描述】:
我想从 DDMS 接收一个浮点值,但它发送一个整数 示例:
经度:2.351696 纬度:48.857593
但我收到:
经度:2.0 纬度:48.0
我怎样才能做到这一点?
这是我的代码:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
if (location != null) {
Toast.makeText(this, "ma position :" + latitude + ", " + longitude,
Toast.LENGTH_LONG).show();
Log.i("latitude", "" + latitude);
Log.i("longitude", "" + longitude);
}
日志:
03-27 09:25:20.163: I/纬度(793): 48.0 03-27 09:25:20.163: I/经度(793): 2.0
private double getDistanceBetweenPoints(double lat1, double long2, double arrive_lat,double arrive_long) {
int R = 6371; // km
double dLat = Math.toRadians(arrive_lat - lat1);
double dLon = Math.toRadians(arrive_long - long2);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(arrive_lat)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double d = R * c;
return d*1000;
}
【问题讨论】:
-
DDMS 是 ADK 调试监视器服务。您只是在 Logcat 输出中看到整数吗?如果是这样,请发布输出消息的代码
-
我使用这些值来计算 2 点 gps 之间的距离,我得到了错误的结果,因此来自 DDMS 的值是错误的。
-
Location.getLatitude()返回一个 double,并且在 logcat (48.0) 中输出一个 double。我会调试你的应用程序并在运行时检查纬度和经度的值。 -
另外,你是在模拟器还是带 GPS 的手机上运行它?
-
Log.i("distance1", ""+this.getDistanceBetweenPoints(48.857593,2.351696,48.858786, 2.348435)); Log.i("distance2", ""+this.getDistanceBetweenPoints(纬度,经度,48.858786, 2.348435));当我输入静态纬度和经度时,第一个距离是正确的,但是当我使用来自 DDMS 的值时,第二个距离是错误的