我正在开发一个 Android 应用程序,我想添加商店定位器详细信息和地图视图。我搜索了很多。但我没有得到任何有目的的东西。我很紧张,强烈需要您的帮助。
问题是,我想求2个坐标之间的距离,我得到了一个半正弦公式,但不知道如何成功运行该程序,因为我是Android初学者,请帮忙做逐步编码.,即xml代码也..,拜托.,拜托.,
来自 MotoDev 片段(GPS 坐标之间的距离):
Location originLocation = new Location("gps");
Location destinationLocation = new Location("gps");
originLocation.setLatitude(originLatitude);
originLocation.setLongitude(originLongitude);
destinationLocation.setLatitude(originLatitude);
destinationLocation.setLongitude(originLongitude);
float distance = originLocation.distanceTo(destinationLocation);
http://developer.android.com/reference/android/location/Location.html
查看 distanceTo 或 distanceBetween。您可以根据纬度和经度创建位置对象:
Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);
使用此代码。您将获得两个地理点之间的距离。
private String distance(double lati, double longi, double curlati1,
double curlongi1) {
double theta = longi - curlongi1;
double dist = Math.sin(deg2rad(lati)) * Math.sin(deg2rad(curlati1))
+ Math.cos(deg2rad(lati)) * Math.cos(deg2rad(curlati1))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
dist = dist * 1.609344;
result = Double.toString(dist);
System.out.println("dist_result :" + result);
return (result);
}
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :: This function converts decimal degrees to radians : */
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :: This function converts radians to decimal degrees : */
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}