use*_*541 15 android android-location android-mapview
嗨我需要在onchangelocation()上调用一个事件,通过比较当前纬度和经度与一些保存的纬度和经度,但我得到一个错误.日食不识别关键词距离,并且对于校正错误它给出提示箱子方法"距离"有4参数...如何解决它?或其他一些方式做同样的工作???
感谢致敬.代码附在下面
@Override
public void onLocationChanged(Location location) {
double currentLat=location.getLatitude();
double currentLon=location.getLongitude();
if (distance(lat,lon,currentLat,currentLon)<2.0){
//do what you want to do...
}
}
Run Code Online (Sandbox Code Playgroud)
Phi*_*oda 31
实际上,您需要实现一个名为distance的函数来计算两个位置之间的距离.计算两个位置之间的距离是比较经度和纬度值的一种可能方式.
比较它们的一个例子:
@Override
public void onLocationChanged(Location location) {
double lat2 = location.getLatitude();
double lng2 = location.getLongitude();
// lat1 and lng1 are the values of a previously stored location
if (distance(lat1, lng1, lat2, lng2) < 0.1) { // if distance < 0.1 miles we take locations as equal
//do what you want to do...
}
}
/** calculates the distance between two locations in MILES */
private double distance(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 3958.75; // in miles, change to 6371 for kilometer output
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
* Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
return dist; // output distance, in MILES
}
Run Code Online (Sandbox Code Playgroud)
yuv*_*val 12
您可以使用Location类的静态distanceBetween()方法,如下所示:
float[] distance = new float[1];
Location.distanceBetween(lat, lon, currentLat, currentLon, distance);
// distance[0] is now the distance between these lat/lons in meters
if (distance[0] < 2.0) {
// your code...
}
Run Code Online (Sandbox Code Playgroud)
如果你有两个Location对象,另一个选项是distanceTo()
| 归档时间: |
|
| 查看次数: |
23892 次 |
| 最近记录: |