【问题标题】:Find the location nearest to the current location from an array of three locations and compare that which one is nearest to the current location从三个位置的数组中找到离当前位置最近的位置,并比较哪个位置离当前位置最近
【发布时间】:2020-02-19 12:45:59
【问题描述】:
ArrayList<LatLng> arrayList= new ArrayList<LatLng>();
LatLng near = new LatLng(33.549232, 73.125256);
LatLng pass = new LatLng(33.549844, 73.125849);
LatLng qareeb = new LatLng(33.549603, 73.124201);                                               


public void onMapReady(GoogleMap googleMap) {
    mMap= googleMap;
    for (int i =0; i<arrayList.size();i++){
        mMap.addMarker(new MarkerOptions().position(arrayList.get(i)).title("Marker").snippet("and snippet")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
        LatLng latLng = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions().position(latLng).title("Main Yahan hn");
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(arrayList.get(i),15));
        mMap.animateCamera(CameraUpdateFactory.zoomIn());
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
        /*mMap.animateCamera(CameraUpdateFactory.zoomTo(15.0f));*/
        mMap.moveCamera(CameraUpdateFactory.newLatLng(arrayList.get(i)));
        googleMap.addMarker(markerOptions);

    }
}

上面是我的代码,我想创建 3 个点来与我当前的位置进行比较,哪个点靠近当前位置。

【问题讨论】:

    标签: android api android-gps


    【解决方案1】:

    您提出的问题和您发布的代码不完整。该代码只是在Activity 上启用GoogleMaps。我不会向您编写整个解决方案,而是向您解释逻辑并希望您在最后尝试一下,并在需要时在此处重新发布更新的答案。

    步骤:

    1. 使用FusedLocationProviderClient 获取设备的当前位置并将值存储在变量中(例如currentLocation)。 (Get the last known location)
    2. 使用Location 类在point1currentLocation 之间查找distance。 (比如d1)。 (distanceBetween)
    3. 使用相同的过程来查找point2point3currentLocation 之间的距离。
    4. 现在您将拥有 3 个 distance 变量,d1d2d3。我想您可以轻松找到其中最短的。

    【讨论】:

      【解决方案2】:

      您可以计算两个位置之间的距离,然后比较距离。

       private double distance(double lat1, double lon1, double lat2, double lon2) {
          double theta = lon1 - lon2;
          double dist = Math.sin(deg2rad(lat1))
                  * Math.sin(deg2rad(lat2))
                  + Math.cos(deg2rad(lat1))
                  * Math.cos(deg2rad(lat2))
                  * Math.cos(deg2rad(theta));
          dist = Math.acos(dist);
          dist = rad2deg(dist);
          dist = dist * 60 * 1.1515;
          return (dist);
      }
      
      private double deg2rad(double deg) {
          return (deg * Math.PI / 180.0);
      }
      
      private double rad2deg(double rad) {
          return (rad * 180.0 / Math.PI);
      }
      

      您可以将其用作以下代码

        LatLng near = new LatLng(33.549232, 73.125256);
              LatLng pass = new LatLng(33.549844, 73.125849);
              LatLng qareeb = new LatLng(33.549603, 73.124201);
              double distance = distance(near.latitude, near.longitude, pass.latitude, pass.longitude);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-22
        • 2022-11-28
        • 2018-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多