简体   繁体   English

从ArrayList获取最近的点<LatLng>

[英]Get nearest point from ArrayList<LatLng>

i'm working on a App that has a Google map and a Polyline to specify a route. 我正在开发一个具有Google地图和折线以指定路线的应用程序。 What i want is that when the user opens the map it automatically put a marker on the nearest point between the user and the polyline. 我想要的是,当用户打开地图时,它会自动在用户和折线之间的最近点上放置一个标记。

I've already set up a function that gets the nearest point from the user to the polyline. 我已经设置了一个函数,该函数获取用户到折线的最近点。 If the distance is < 600 it puts a marker in that point. 如果距离<600,则会在该点放置一个标记。 But what i want is to take all the Coordinates(of the polyline) that are < 600 meters from the user and choose the closest one... this is my code 但是我想要的是获取距用户<600米的所有(折线的)坐标并选择最接近的坐标...这是我的代码

polyline ArrayList: 折线ArrayList:

 public ArrayList<LatLng> polylineList(){

    ArrayList<LatLng> coords = new ArrayList<LatLng>();

    coords.add(new LatLng(25.71687391423798,-100.3730825815284));
    coords.add(new LatLng(25.71682182829175,-100.3733097782636));
    coords.add(new LatLng(25.71678126641878,  -100.374090669652));
    coords.add(new LatLng(25.71683201122758, -100.3731781269444));
    coords.add(new LatLng(25.72018196813817, -100.3735084186905));
    coords.add(new LatLng(25.72031604576068,  -100.3736959395207));
    coords.add(new LatLng(25.71998332400039,   -100.3762802484946));
    coords.add(new LatLng(25.71994816609866,   -100.3764646004368));
    coords.add(new LatLng(25.72208094229626,   -100.3773901496331));
    coords.add(new LatLng(25.72139701948525, -100.3784182403878));
    coords.add(new LatLng(25.72088085424743, -100.3792655793197));
    coords.add(new LatLng(25.7211341509678, -100.3796660319109));
    coords.add(new LatLng(25.72119368137114,  -100.3807655073227));
    coords.add(new LatLng(25.72102639768713,  -100.3811907891904));
    coords.add(new LatLng(25.72079498457438,   -100.3810006195533));
    coords.add(new LatLng(25.720670392499,   -100.3799969462212));
    coords.add(new LatLng(25.72062014771428, -100.3798718252661));
    coords.add(new LatLng(25.71970136267208,  -100.3812225396649));
    coords.add(new LatLng(25.71935874972525,   -100.3814558417726));
    coords.add(new LatLng(25.71758071083912,    -100.3839700351668));
    coords.add(new LatLng(25.71722347802044,   -100.3838886540422));
    coords.add(new LatLng(25.71531766471047,    -100.3815788239292));
    coords.add(new LatLng(25.71564916096481,    -100.3804237189767));
    coords.add(new LatLng(25.7159267510913,   -100.3800793191019));
    coords.add(new LatLng(25.71627234185268,   -100.3795543063391));
    coords.add( new LatLng(25.71665021282444,    -100.3790150094068));

return coords; 返回坐标; } }

Then, i get the user's location and evaluate the coordinates with every ArrayList Index using the HAVERSINE formula, if the distace is <600 it will draw a marker. 然后,我得到用户的位置并使用HAVERSINE公式使用每个ArrayList索引评估坐标,如果距离小于600,它将绘制一个标记。

 for (int i = 0; i < polylineList().size(); i++) {
                    Double latit = polylineList.get(i).latitude;
                    Double longit = polylineList.get(i).longitude;
                    LatLng newLatLng = new LatLng(latit, longit);
                    Double distance = distanceHaversine(userlatit, latit, userlongit, longit);
                    if (distance < 600) {
                        drawMarker(newLatLng);
                    }
                }

this is working correctly but it draws a lot of markers in the coordinates that are < 600 meters from the user's location. 这可以正常工作,但是会在距用户位置<600米的坐标中绘制许多标记。 What i want is to draw a marker only in the nearest point of the polyline. 我想要的是仅在折线的最近点绘制一个标记。 Some sort of evaluate all the coords that are < 600 from the user and then choose the closest. 对用户评估所有小于600的坐标,然后选择最接近的坐标。 Appreciate any help. 感谢任何帮助。

find minimum distance from user to available points, get the latlng associated with min distance and draw marker using that 找到用户到可用点的最小距离,获取与最小距离相关的latlng ,并使用该距离绘制marker

private int minDistance = 0;
    private LatLng closestLatLng = null;
     for (int i = 0; i < polylineList().size(); i++) {
                        Double latit = polylineList.get(i).latitude;
                        Double longit = polylineList.get(i).longitude;
                        LatLng newLatLng = new LatLng(latit, longit);
                        Double distance = distanceHaversine(userlatit, latit, userlongit, longit);
                        if (distance < minDistance) {
                            closestLatLng = newLatLng;
                            minDistance = distance;
                        }
                    }
    if(closestLatLng !=null && minDistance < 600){
        drawMarker(closestLatLng);
    }

There is Map Utils class for map by using that you can find location that is at particular distance from your current location on polyline 通过使用Map的Map Utils类,您可以找到距折线上当前位置特定距离的位置

if (PolyUtil.isLocationOnPath(start, points, true, 10)) {
      // do your stuf here
}

here start is your current location, points is array of polyline points and last parameter is for range(distance) from your path in meter. 这里的开始是您的当前位置,点是多义线点的数组,最后一个参数是距您的路径的距离(距离)(以米为单位)。

for this you have to add this dependancy into your gradle. 为此,您必须将此依赖项添加到gradle中。

compile 'com.google.maps.android:android-maps-utils:0.5+'

hope this helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM