我正在开发一个 Android 应用程序,我想添加商店定位器详细信息和地图视图。我搜索了很多。但我没有得到任何有目的的东西。我很紧张,非常需要你的帮助。
问题是,我想求2个坐标之间的距离,我得到了一个Haversine公式。,但是不知道如何成功运行程序。,由于我是Android初学者,请帮忙做逐步编码。,即xml代码也是..,请。,请。,
如上所述,位置类是要走的路。
这是我使用的代码:
Location locationA = new Location("point A");
locationA.setLatitude(pointA.getLatitudeE6() / 1E6);
locationA.setLongitude(pointB.getLongitudeE6() / 1E6);
Location locationB = new Location("point B");
locationB.setLatitude(pointB.getLatitudeE6() / 1E6);
locationB.setLongitude(pointB.getLongitudeE6() / 1E6);
double distance = locationA.distanceTo(locationB);
在此示例中,pointA和pointB都是 GeoPoint 类的实例。
从 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);
使用此代码。您将获得两个地理点之间的距离。
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);
}
http://developer.android.com/reference/android/location/Location.html
查看 distanceTo 或 distanceBetween。您可以根据纬度和经度创建 Location 对象:
Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);