Java 距离我的GPS坐标最近的OSM(开放式街道地图)节点

Java 距离我的GPS坐标最近的OSM(开放式街道地图)节点,java,gps,latitude-longitude,openstreetmap,Java,Gps,Latitude Longitude,Openstreetmap,我正在使用开放式街道地图。现在我可以得到地图上所有的交点,也可以得到它们的经度和纬度。。我的应用程序中也有GPS 我要做的是找到离我的GPS坐标最近的交点 这是我的方法,但不太准确。我只是想知道为什么!谢谢 public Nodee nearestPoint(double longitude, double latitude) throws Exception { inters = new ArrayList<Nodee>(); inters = getInterse

我正在使用开放式街道地图。现在我可以得到地图上所有的交点,也可以得到它们的经度和纬度。。我的应用程序中也有GPS 我要做的是找到离我的GPS坐标最近的交点 这是我的方法,但不太准确。我只是想知道为什么!谢谢

public Nodee nearestPoint(double longitude, double latitude) throws Exception {
    inters = new ArrayList<Nodee>();
    inters = getIntersections();

    double minLong = Math.abs(longitude - (inters.get(0).lon));
    double minLat = Math.abs(latitude - (inters.get(0).lat));
    Nodee NearestNodeLong = null;
    Nodee NearestNodeLat = null;

    for (int i = 0; i < inters.size(); i++) {
        if (Math.abs((inters.get(i).lon) - longitude) < minLong) {
            minLong = (Math.abs((inters.get(i).lon) - longitude));
            NearestNodeLong = inters.get(i);
    } 
        if (Math.abs((inters.get(i).lat) - latitude) < minLat) {
            minLat = Math.abs((inters.get(i).lat) - latitude);
            NearestNodeLat = inters.get(i);
        } 

    }
    if (NearestNodeLong.equals(NearestNodeLat)) {
        return NearestNodeLong;
    } else if (!NearestNodeLong.equals(NearestNodeLat)) {
        Double d1 = (double) distFrom(latitude, longitude, (NearestNodeLat.lat),
                (NearestNodeLat.lon));

        Double d2 = (double) distFrom(latitude, longitude, (NearestNodeLong.lat),
                (NearestNodeLong.lon));

        if (d1 < d2)
            return NearestNodeLat;
        else if (d2 < d1)
            return NearestNodeLong;

    }
    return null;

}
公共节点最近点(双经度、双纬度)引发异常{
inters=新阵列列表();
inters=getcrossions();
double minLong=Math.abs(经度-(inters.get(0.lon));
double minLat=Math.abs(纬度-(inters.get(0.lat));
Nodee NearestNodeLong=null;
Nodee NearestNodeLat=null;
对于(int i=0;i
尝试以下解决方案:

public Nodee nearestPoint(double longitude, double latitude) throws Exception {
    Nodee result = null;
    double maxDifference = 0.1;
    ArrayList<Nodees> filteredNodes = getBox(longitude, latitude, maxDifference);

    if ( filteredNodes.size()<=0) return null;
    double minDistance = calcDistance(longitude, latitude,
                    filteredNodes.get(0).lon,filteredNodes.get(0).lon);
    result = filteredNodes.get(0);
    for (int i=1; i<filteredNodes.size();i++){
          Nodee current = filteredNodes.get(i);
          double diffDistance = calcDistance(longitude, latitude,
                            filteredNodes.get(i).lon,filteredNodes.get(i).lon);
          if( minDistance > diffDistance){
                minDistance = diffDistance;
                result = current;
          }
    }   
    return result;
}

public ArrayList<Nodee> getBox(double lon, double lat, double diff) throws Exception{
    ArrayList<Nodee> inters = getIntersections();
    ArrayList<Nodee> result = new ArrayList<Nodee>();
    for (int i=0; i<inters.size(); i++){
        if(Math.abs(inters.get(i).lat-lat)<=diff &&
           Math.abs(inters.get(i).lon-lon)<=diff)
                result.add(inters.get(i));
    }   
    return result();
}

public double calcDistance(lon1, lat1, lon2, lat2){
    deltaLon = lon1-lon2;
    deltaLat = lat1-lat2;
    return Math.sqrt(deltaLon*deltaLon+deltaLat*deltaLat);
}
公共节点最近点(双经度、双纬度)引发异常{
节点结果=null;
双最大差值=0.1;
ArrayList filteredNodes=getBox(经度、纬度、最大差值);
if(filteredNodes.size()的值)