测量 Android 应用程序中两个坐标之间的距离
我正在尝试测量两个坐标之间的距离,我已经实现了半正矢函数,就像这样:
package Services;
public class Route {
private double latitude1;
private double longitude1;
private double latitude2;
private double longitude2;
public Route(double latitude1, double longitude1, double latitude2, double longitude2) {
this.latitude1 = latitude1;
this.longitude1 = longitude1;
this.latitude2 = latitude2;
this.longitude2 = longitude2;
}
public double measureDistance() {
double earthRadius = 3958.75;
double dLat = Math.toRadians(latitude2-latitude1);
double dLng = Math.toRadians(longitude2-longitude1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(latitude1)) * Math.cos(Math.toRadians(latitude2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
return dist;
}
}
代码运行良好并输出一些内容。但当我使用半正矢公式时,代码只测量天际线的距离,而不是道路,如果你明白我的意思的话。有没有像谷歌地图一样测量距离的方法?在不使用谷歌地图 API 的情况下,谷歌突然在一天内调用 2500 个 API 后就收取了现金。
I'am trying to measure the distance between two coordinates, i've implemented the haversine function, just like this:
package Services;
public class Route {
private double latitude1;
private double longitude1;
private double latitude2;
private double longitude2;
public Route(double latitude1, double longitude1, double latitude2, double longitude2) {
this.latitude1 = latitude1;
this.longitude1 = longitude1;
this.latitude2 = latitude2;
this.longitude2 = longitude2;
}
public double measureDistance() {
double earthRadius = 3958.75;
double dLat = Math.toRadians(latitude2-latitude1);
double dLng = Math.toRadians(longitude2-longitude1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(latitude1)) * Math.cos(Math.toRadians(latitude2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
return dist;
}
}
The code works fine and ouputting something. But as I use the haversine formula, the code just measure the distance in skyline, not the roads if you know what I mean. Is there any possible way to measure distance like google maps does? Without using the google maps API, suddly google takes cash after 2500 API calls in a day or something..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Google Maps API 定价基本上只影响网络地图:http://code.google.com /apis/maps/faq.html#usage_apis
Android 版 Google 地图 API 仍然免费。
OTOH,Android 版 Google 地图中没有路线或导航功能,因此无法使用此 API 计算路线距离。
另外,您可能会发现这很方便:地理距离
Google Maps API pricing basically only affects web maps: http://code.google.com/apis/maps/faq.html#usage_apis
Google Maps for Android API is still free.
OTOH, there is no routing or navigation functionality in Google Maps for Android, so there is no way to calculate route distance with this API.
Also, you might find this handy: Geographical distance
您应该尝试 Bing 地图 Android SDK。他们没有明确的使用配额。
You should try the Bing Maps Android SDK. They don't have an explicit usage quota.
Bing 地图确实有使用配额,但这不适用于移动应用程序。 Bing 地图绝对值得研究。您可以通过以下两种方式之一将 Bing 地图与 Android 结合使用。第一种是使用 Bing Maps Android SDK:http://bingmapsandroidsdk.codeplex.com/
第二种是将Bing Maps V7 AJAX控件与PhoneGap结合使用。
Bing Maps does have a usage quota, however this does not apply for mobile apps. Bing Maps is definatelty worth looking into. You can use Bing Maps with Android in one of two ways. The first is to use the Bing Maps Android SDK: http://bingmapsandroidsdk.codeplex.com/
The second is to use Bing Maps V7 AJAX control with PhoneGap.