【问题标题】:Get nearest point from ArrayList<LatLng>从 ArrayList<LatLng> 获取最近的点
【发布时间】:2017-08-21 04:29:13
【问题描述】:

我正在开发具有 Google 地图和折线以指定路线的应用程序。我想要的是,当用户打开地图时,它会自动在用户和折线之间的最近点上放置一个标记。

我已经设置了一个函数来获取从用户到折线的最近点。如果距离

折线数组列表:

 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));

返回坐标; }

然后,我获取用户的位置并使用 HAVERSINE 公式评估每个 ArrayList 索引的坐标,如果距离

 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);
                    }
                }

这工作正常,但它在距离用户位置

【问题讨论】:

  • 你有没有找到解决方案

标签: java android google-maps arraylist haversine


【解决方案1】:

有用于地图的 Map Utils 类,您可以使用它在折线上找到与当前位置特定距离的位置

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

这里 start 是您当前的位置,points 是折线点数组,最后一个参数是距离您的路径的范围(距离),以米为单位。

为此,您必须将此依赖项添加到您的 gradle 中。

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

希望对你有所帮助。

【讨论】:

  • 感谢您的回答。这将获得 10 米距离内的所有点,但是如果在 10 米处有很多点怎么办......我只想找到离用户最近的点......
  • @DiegoTrujillo 您是如何获得解决方案的?
【解决方案2】:

找到用户到可用点的最小距离,获取与最小距离相关联的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);
    }

【讨论】:

  • 感谢您的回复,我不明白为什么:如果(距离
  • 什么是distanceHaverine()方法?
猜你喜欢
  • 2013-05-02
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-17
  • 2017-11-24
相关资源
最近更新 更多