0

我得到以下代码,我从 XML 文件中读取 GPS 坐标,然后我想检查我当前的位置(纬度和经度)是否在这个多边形区域内......不确定是否做错了:(看起来像我的 IsInsideRegion 方法失败了欢迎任何建议。

公共类点{

private double _latitude = 0d;
private double _longitude = 0d;

public Point() {
}

public Point(double aLatitude, double aLongitude) {
    _latitude = aLatitude;
    _longitude = aLongitude;
}

public Point(String pointData) throws Exception {
    FromString(pointData);
}

public static String ToString(Point point) {
    StringBuilder result = new StringBuilder();
    result.append(point._latitude).append(",").append(point._longitude);
    return result.toString();
}

public void FromString(String pointData) throws Exception {

    String[] fields = pointData.split(",");
    if (fields.length != 2)
        throw new FormatException("Invalid input data: " + pointData + " in " + this.getClass().getName());

    _latitude = parseDouble(fields[0]);
    _longitude = parseDouble(fields[1]);
}

private double parseDouble(String aValue) {
    double result = 0d;
    try {
        result = Double.parseDouble(aValue);
    } catch (Exception ex) {
    }

    return result;
}

public double getLatitude() {
    return _latitude;
}

public double getLongitude() {
    return _longitude;
}


public boolean IsInsideRegion(double latitude, double longitude)
{

    //Log.d("<GPD><POLY>","PR :" + String.valueOf(_pointList));

    Boolean result = false;
    if (_pointList != null && _pointList.size() > 0) {
        int Index1 = 0;
        int Index2 = _pointList.size() - 1;

        while (Index1 < _pointList.size())
        {
            if ((((_pointList.get(Index1).getLongitude() <= longitude) && (longitude < _pointList.get(Index2).getLongitude())) || ((_pointList.get(Index2).getLongitude() <= longitude) && (longitude < _pointList.get(Index1).getLongitude()))))
            {
                if (latitude < (_pointList.get(Index2).getLatitude() - _pointList.get(Index1).getLatitude()) * (longitude - _pointList.get(Index1).getLongitude()) / (_pointList.get(Index2).getLongitude() - _pointList.get(Index1).getLongitude()) + _pointList.get(Index1).getLatitude())
                    result = !result;
            }

            Index2 = Index1;
            Index1++;
        }
    }
    if (result)
        Log.d("<GPD><POLY>", "In Polygon Region:" + this.getId());
    return result;
}

private void calculateMinMaxLatLong(double aMinLatitude, double aMaxLatitude, double aMinLongitude, double aMaxLongitude)
{
    /*
    _minLatitude = 0d;
    _maxLatitude = 0d;
    _minLongitude = 0d;
    _maxLongitude = 0d;

    for (Point currentPoint: _pointList)
    {
        if (currentPoint.getLatitude() < aMinLatitude)
            _minLatitude = currentPoint.getLatitude();

        if (currentPoint.getLatitude() > aMaxLatitude)
            _maxLatitude = currentPoint.getLatitude();

        if (currentPoint.getLongitude() < aMinLongitude)
            _minLongitude = currentPoint.getLongitude();

        if (currentPoint.getLongitude() > aMaxLongitude)
            _maxLongitude = currentPoint.getLongitude();
    }*/

    double smallestLat = _pointList.get(0).getLatitude();
    double largestLat = _pointList.get(0).getLatitude();

    double smallestLong = _pointList.get(0).getLongitude();
    double largestLong = _pointList.get(0).getLongitude();

    for (int x = 0; x < _pointList.size(); x++){

        if(smallestLat > _pointList.get(x).getLatitude())
            smallestLat = _pointList.get(x).getLatitude();

        if(largestLat < _pointList.get(x).getLatitude())
            largestLat = _pointList.get(x).getLatitude();

        if(smallestLong > _pointList.get(x).getLongitude())
            smallestLong = _pointList.get(x).getLongitude();

        if(largestLong < _pointList.get(x).getLongitude())
            largestLong = _pointList.get(x).getLongitude();
    }

    _minLatitude = smallestLat;
    _maxLatitude = largestLat;
    _minLongitude = smallestLong;
    _maxLongitude = largestLong;
}

公共 RegionChecker() {}

public RegionChecker(boolean aEnterFirstOnly) {
    _enterFirstOnly = aEnterFirstOnly;
}

public List<Region> CheckGpsPosition(List<Region> aActiveRegion, List<Region> aRegionList, MyLocation aLocation, IRegionEventListener aListener) {
    List<Region> result = new ArrayList<Region>();
    Log.i("RegionChecker", "aRegionList " + String.valueOf(aRegionList.size()));

    boolean regionChanges = false;
    List<Region> overlappingPositionsList = new Regions().InsideRegions(aRegionList, aLocation.getLatitude(), aLocation.getLongitude());

    if (_enterFirstOnly)
    {
        if (overlappingPositionsList != null && overlappingPositionsList.size() > 0)
        {
            Region firstRegion = overlappingPositionsList.get(0);
            overlappingPositionsList.clear();
            overlappingPositionsList.add(firstRegion);
        }
    }

    Log.i("RegionChecker", "_activeRegions " + String.valueOf(aActiveRegion.size()));
    Log.i("RegionChecker", "overlappingPositionsList " + String.valueOf(overlappingPositionsList.size()));

    for(Region currentRegion: overlappingPositionsList) {

        Log.i("overlap check", String.valueOf(aActiveRegion.contains(currentRegion)));

        if (!aActiveRegion.contains(currentRegion)) {
            regionChanges = true;
            aListener.onRegionEnter(currentRegion, aLocation);
        }
    }

    for(Region currentRegion: aActiveRegion) {
           if (!overlappingPositionsList.contains(currentRegion)) {
               regionChanges = true;
               aListener.OnRegionExit(currentRegion, aLocation);
           }
    }

    result = overlappingPositionsList;

    if (regionChanges)
        aListener.OnCurrentRegionsChanged(result);

    return result;
}
4

1 回答 1

0

我认为您必须修改您的区域检查器。

  • 它应该使用经度或纬度,但不能同时使用两者。所以它会检查水平光线有多少次穿过顶点之间的线。或者它检查垂直线的次数。
  • 它需要处理点对和索引 0 & 1、1 & 2 ... n-2 & n-1 和 n-1 & 0。

我没有看到你最后一对在哪里做的,但是有很多代码要阅读。

于 2013-01-23T23:23:05.183 回答