确定是否已到达航路点,以及是否';错过了,用android谷歌地图继续下一个

确定是否已到达航路点,以及是否';错过了,用android谷歌地图继续下一个,android,location,google-maps-android-api-2,distance,Android,Location,Google Maps Android Api 2,Distance,我编写此方法是为了确定沿路径到目的地的距离,以及沿该路径到下一个航路点的距离。问题是,有时我不去距离航路点10米以内的地方,所以不会被发现已经到达。当然,我可以增加距离的阈值,以使航路点满足要求,但我宁愿想出另一个解决方案来确定我是否已通过该航路点,只需跳过它(通过将其添加到航路点搜索列表)即可进入下一个航路点。检查从我的位置到航路点的距离是否在增加而不是减少是不起作用的,因为我可能在到达航路点之前偏离路径 // method to calculate distance to desti

我编写此方法是为了确定沿路径到目的地的距离,以及沿该路径到下一个航路点的距离。问题是,有时我不去距离航路点10米以内的地方,所以不会被发现已经到达。当然,我可以增加距离的阈值,以使航路点满足要求,但我宁愿想出另一个解决方案来确定我是否已通过该航路点,只需跳过它(通过将其添加到航路点搜索列表)即可进入下一个航路点。检查从我的位置到航路点的距离是否在增加而不是减少是不起作用的,因为我可能在到达航路点之前偏离路径

    // method to calculate distance to destinatoin from a loaded route
private void CalculateRouteToDestination(Location location) {

    new AsyncTask<Location, Void, ArrayList<Double>>(){
        @Override
        protected ArrayList<Double> doInBackground(Location... params) {
            ArrayList<Double> distances = new ArrayList<Double>();
            Location location = params[0];
            //predefinedRoutePoints
            int closestLocationIndex = 0;
            double smallestDistance = -1;
            int cnt = 0;
            for(LatLng ltlg : predefinedRoutePoints) {
                // create  new location object for the latlng coords
                Location cLocation = new Location("");
                cLocation.setLatitude(ltlg.latitude);
                cLocation.setLongitude(ltlg.longitude);
                // get the distance from current location to all of the points in the array and set the index from the array where it is
                double cDistance = location.distanceTo(cLocation);
                if(smallestDistance == -1 || cDistance < smallestDistance) {
                    closestLocationIndex = cnt;
                    smallestDistance = cDistance;
                }
                cnt++;
            }
            // now with the index from the array of the cloest point, calculate the distance to the last point in the array from that index (distance to destination)
            double distanceToDestination = 0;
            for(int i = closestLocationIndex; i < predefinedRoutePoints.size(); i++) {
                //Log.d("home","iteration:"+i);
                if(i != (predefinedRoutePoints.size() -1)) {
                    Location l1 = new Location("");
                    l1.setLatitude(predefinedRoutePoints.get(i).latitude);
                    l1.setLongitude(predefinedRoutePoints.get(i).longitude);

                    Location l2 = new Location("");
                    l2.setLatitude(predefinedRoutePoints.get(i+1).latitude);
                    l2.setLongitude(predefinedRoutePoints.get(i+1).longitude);
                    distanceToDestination += l1.distanceTo(l2);

                }
            }
            // now add the distance the user is to the cloest point to get the final output of distance to destinatoin
            distanceToDestination += smallestDistance;
            distances.add(distanceToDestination);

            // check if need to measure distance to next waypoint
            double distanceToNextWaypoint = 0;
            if(predefinedWaypoints.size() > 0){
                if(predefinedWaypoints.size() != waypointsReached.size()) {
                    for (int i = closestLocationIndex; i < predefinedWaypoints.get(waypointsReached.size()).WaypointIndex; i++) {
                        Location l1 = new Location("");
                        l1.setLatitude(predefinedRoutePoints.get(i).latitude);
                        l1.setLongitude(predefinedRoutePoints.get(i).longitude);

                        Location l2 = new Location("");
                        l2.setLatitude(predefinedRoutePoints.get(i + 1).latitude);
                        l2.setLongitude(predefinedRoutePoints.get(i + 1).longitude);
                        distanceToNextWaypoint += l1.distanceTo(l2);
                    }

                    distanceToNextWaypoint += smallestDistance;
                    if(distanceToNextWaypoint < 10) {
                        waypointsReached.add(predefinedWaypoints.get(waypointsReached.size()));

                    }
                    distances.add(distanceToNextWaypoint);
                }
            }
            return distances;
        }
        @Override
        protected void onPostExecute(ArrayList<Double> v) {
            distanceToDestinationText.setText("Destination To Destination: "+Utilities.GetMiles(v.get(0))+"mi");
            if(v.size() > 1) {
                distanceToNextWaypoint.setText("Next Waypoint: "+Utilities.GetMiles(v.get(1))+"mi");
            } else {
                distanceToNextWaypoint.setVisibility(View.GONE);
                distanceToNextWaypoint.setText("Next Waypoint: 0.0mi");
            }
        }
    }.execute(location);
}
有谁能帮我想一个办法,我可以确定我是否已经通过了这个航路点,然后继续下一个航路点

这是我写的一个方法,用来计算到最终目的地和下一个航路点的距离

    // method to calculate distance to destinatoin from a loaded route
private void CalculateRouteToDestination(Location location) {

    new AsyncTask<Location, Void, ArrayList<Double>>(){
        @Override
        protected ArrayList<Double> doInBackground(Location... params) {
            ArrayList<Double> distances = new ArrayList<Double>();
            Location location = params[0];
            //predefinedRoutePoints
            int closestLocationIndex = 0;
            double smallestDistance = -1;
            int cnt = 0;
            for(LatLng ltlg : predefinedRoutePoints) {
                // create  new location object for the latlng coords
                Location cLocation = new Location("");
                cLocation.setLatitude(ltlg.latitude);
                cLocation.setLongitude(ltlg.longitude);
                // get the distance from current location to all of the points in the array and set the index from the array where it is
                double cDistance = location.distanceTo(cLocation);
                if(smallestDistance == -1 || cDistance < smallestDistance) {
                    closestLocationIndex = cnt;
                    smallestDistance = cDistance;
                }
                cnt++;
            }
            // now with the index from the array of the cloest point, calculate the distance to the last point in the array from that index (distance to destination)
            double distanceToDestination = 0;
            for(int i = closestLocationIndex; i < predefinedRoutePoints.size(); i++) {
                //Log.d("home","iteration:"+i);
                if(i != (predefinedRoutePoints.size() -1)) {
                    Location l1 = new Location("");
                    l1.setLatitude(predefinedRoutePoints.get(i).latitude);
                    l1.setLongitude(predefinedRoutePoints.get(i).longitude);

                    Location l2 = new Location("");
                    l2.setLatitude(predefinedRoutePoints.get(i+1).latitude);
                    l2.setLongitude(predefinedRoutePoints.get(i+1).longitude);
                    distanceToDestination += l1.distanceTo(l2);

                }
            }
            // now add the distance the user is to the cloest point to get the final output of distance to destinatoin
            distanceToDestination += smallestDistance;
            distances.add(distanceToDestination);

            // check if need to measure distance to next waypoint
            double distanceToNextWaypoint = 0;
            if(predefinedWaypoints.size() > 0){
                if(predefinedWaypoints.size() != waypointsReached.size()) {
                    for (int i = closestLocationIndex; i < predefinedWaypoints.get(waypointsReached.size()).WaypointIndex; i++) {
                        Location l1 = new Location("");
                        l1.setLatitude(predefinedRoutePoints.get(i).latitude);
                        l1.setLongitude(predefinedRoutePoints.get(i).longitude);

                        Location l2 = new Location("");
                        l2.setLatitude(predefinedRoutePoints.get(i + 1).latitude);
                        l2.setLongitude(predefinedRoutePoints.get(i + 1).longitude);
                        distanceToNextWaypoint += l1.distanceTo(l2);
                    }

                    distanceToNextWaypoint += smallestDistance;
                    if(distanceToNextWaypoint < 10) {
                        waypointsReached.add(predefinedWaypoints.get(waypointsReached.size()));

                    }
                    distances.add(distanceToNextWaypoint);
                }
            }
            return distances;
        }
        @Override
        protected void onPostExecute(ArrayList<Double> v) {
            distanceToDestinationText.setText("Destination To Destination: "+Utilities.GetMiles(v.get(0))+"mi");
            if(v.size() > 1) {
                distanceToNextWaypoint.setText("Next Waypoint: "+Utilities.GetMiles(v.get(1))+"mi");
            } else {
                distanceToNextWaypoint.setVisibility(View.GONE);
                distanceToNextWaypoint.setText("Next Waypoint: 0.0mi");
            }
        }
    }.execute(location);
}
//计算从装载的路线到目的地的距离的方法
专用无效计算器OuteToDestination(位置){
新建异步任务(){
@凌驾
受保护的ArrayList doInBackground(位置…参数){
ArrayList距离=新的ArrayList();
位置=参数[0];
//预定义路由点
int closestLocationIndex=0;
双最小距离=-1;
int-cnt=0;
用于(LatLng ltlg:预定义的路由点){
//为latlng坐标创建新的位置对象
地点位置=新地点(“”);
设置纬度(ltlg.纬度);
设置经度(ltlg经度);
//获取从当前位置到阵列中所有点的距离,并从其所在的阵列设置索引
双C距离=位置。距离(闭合);
if(最小距离==-1 | | Cd距离<最小距离){
closestLocationIndex=cnt;
最小距离=距离;
}
cnt++;
}
//现在使用最接近点数组中的索引,计算从该索引到数组中最后一个点的距离(到目标的距离)
双距离目标=0;
对于(int i=closestLocationIndex;i0){
if(预定义的航路点.size()!=航路点缓存的.size()){
对于(int i=closestLocationIndex;i1){
distanceToNextWaypoint.setText(“下一个航路点:+Utilities.GetMiles(v.get(1))+“mi”);
}否则{
distanceToNextWaypoint.setVisibility(View.GONE);
距离到下一个航路点.setText(“下一个航路点:0.0mi”);
}
}
}.执行(地点);
}

您好,您找到解决方案了吗?很抱歉延迟响应。我没有找到解决办法。我刚从一次背包旅行回来,只是把门槛设置得更高一些(比如20米),这对我来说很有效。嗨,你找到解决办法了吗?很抱歉反应延迟。我没有找到解决办法。我刚从一次背包旅行回来,只是把门槛设置得更高一些(比如20米),这对我来说很有效。