计算两个位置之间的距离。仅当经度更改时值才会更改
我正在使用这段代码来计算两点之间的距离。我正在使用半正弦公式,
private double CalcDistance(Location Start, Location End)
{
// TODO Auto-generated method stub
double distance;
double lat1 = Start.getLatitude();
double lat2 = End.getLatitude();
double lng1 = Start.getLongitude();
double lng2 = End.getLongitude();
double dLat = Math.abs(Math.toRadians(lat2-lat1));
double dLng = Math.abs(Math.toRadians(lng2 - lng1));
distance = Math.sin(dLat/2) * Math.sin(dLng/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.asin(Math.sqrt(distance));
//Return the answer in Kilometre (6371km the mean radius of the earth)
return c * 6371;
}
仅当位置的经度更改时生成的值才会更改,并且如果纬度更改则保持不变。我的代码只这样做有什么原因吗?
I am using this code to calculate the the distance between two points. I am using the Haversine formula
private double CalcDistance(Location Start, Location End)
{
// TODO Auto-generated method stub
double distance;
double lat1 = Start.getLatitude();
double lat2 = End.getLatitude();
double lng1 = Start.getLongitude();
double lng2 = End.getLongitude();
double dLat = Math.abs(Math.toRadians(lat2-lat1));
double dLng = Math.abs(Math.toRadians(lng2 - lng1));
distance = Math.sin(dLat/2) * Math.sin(dLng/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.asin(Math.sqrt(distance));
//Return the answer in Kilometre (6371km the mean radius of the earth)
return c * 6371;
}
The valus that is produced only changes if the Longitude of the Location is changed and stays the same if the Latitude is changed. Is there any reason my code only does this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嘿,我不完全确定,但只要看看你的代码,你可能犯了一个错误,我不明白,但也许你可以
改为
Hey I'm not entirely sure, but just looking at your code you might have made an error, I dont understand it but perhaps you could change
to
你需要更改
为
You need to change
to