使用经纬度比较两个位置

21

你好,我需要在onchangelocation()中通过比较当前纬度和经度与一些保存的纬度和经度来调用事件,但是我遇到了一个错误。Eclipse无法识别关键字"distance",并且为了纠正错误,它提示创建一个具有4个参数的方法"distance"...如何解决?还有其他方法可以完成相同的工作吗?

谢谢,并附上代码:

@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...
  }
}

2
你需要编写一个名为distance的函数,因为它在Android中不存在。你可能还想查看这个链接:http://developer.android.com/training/location/geofencing.html - Ran
先生,我只想比较两个纬度和经度的值。如果匹配,则调用一个事件,否则调用另一个事件。有没有其他简单的方法可以做到这一点... 这是我的完整代码:http://stackoverflow.com/questions/18150929/how-to-call-event-depening-upon-latitide-and-longitude/18152841?noredirect=1#18152841 - user2590541
所以问题在于distance方法。你能否发布distance方法的签名? - Sandeep
问题在于他没有距离方法。 - Philipp Jahoda
3个回答

42

您需要实现一个名为 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
}

而在 Kotlin 中:

private fun distance(lat1: Double, lng1: Double, lat2: Double, lng2: Double): Double {
    val earthRadius = 3958.75 // in miles, change to 6371 for kilometer output

    val dLat = Math.toRadians(lat2 - lat1)
    val dLng = Math.toRadians(lng2 - lng1)

    val sindLat = sin(dLat / 2)
    val sindLng = sin(dLng / 2)

    val a = sindLat.pow(2.0) +
            (sindLng.pow(2.0) * cos(Math.toRadians(lat1)) * cos(Math.toRadians(lat2)))

    val c = 2 * atan2(sqrt(a), sqrt(1 - a))

    return earthRadius * c // output distance, in MILES
}

先生,我只想比较两个纬度和经度的值。如果匹配,则调用一个事件,否则调用另一个事件。有没有其他简单的方法可以做到这一点... 这是我的完整代码http://stackoverflow.com/questions/18150929/how-to-call-event-depening-upon-latitide-and-longitude/18152841?noredirect=1#18152841 - user2590541
1
那么你应该表达出来,而不是使用一个不存在的名为distance的函数。 - Philipp Jahoda
我尝试过,但是出现了错误...您能否为此目的编写一个示例代码? - user2590541
3
在我的代码中已经给出了完整的示例。该方法计算两个位置之间的距离,并且当然也可以用于比较。例如,如果两个位置之间的距离小于0.01英里,您可以将它们视为相等。 - Philipp Jahoda
我尝试了一下,但它没有给出期望的输出,请检查一下我做错了什么... 我有一个子类,在其中我正在处理onlocationchanges(),然后我在主活动中调用它。 当我在主函数中调用它时,它显示参数错误,我尝试通过以下两种可能性进行修复:
  1. 给参数null
  2. 给参数值Location
但在这两种情况下,它都会终止我的应用程序。您有什么想法吗?
- user2590541
显示剩余2条评论

17

您可以像这样使用 Location 类的静态 distanceBetween() 方法:

import android.location.Location

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...
}

如果您有两个Location对象,则另一种选择是distanceTo()


0
所有4个输入参数都是浮点数。 距离是相对的,而不是真实的距离。您需要通过在网上搜索一些公式来将此距离转换为真实距离 :( 我在我的应用程序中使用它来获取最近的地铁站从我的当前位置。希望这个片段能帮助到某人 :)
float distance(float lat,float lon,float clat,float clon)
{
    float distance;
    float temp1;
    float temp2;
    temp1=(float)((lat-clat)*(lat-clat));
    temp2=(float)((lon-clon)*(lon-clon));
    distance=temp1+temp2;
    return distance;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接