2016-06-02 10 views
0

私はアンドロイドスタジオのGoogleマップ上の2つの場所の距離を見つける方法を知っていますが、どの距離が3または4の場所を見つけて最も近いものかを計算する方法は?最も近い場所を計算GoogleマップAndroid Studio

public void onDirectionFinderSuccess(List<Route> routes) { 
    progressDialog.dismiss(); 
    polylinePaths = new ArrayList<>(); 
    originMarkers = new ArrayList<>(); 
    destinationMarkers = new ArrayList<>(); 

    for (Route route : routes) { 
     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 16)); 
     ((TextView) findViewById(R.id.tvDuration)).setText(route.duration.text); 
     ((TextView) findViewById(R.id.tvDistance)).setText(route.distance.text); 

     originMarkers.add(mMap.addMarker(new MarkerOptions() 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue)) 
       .title(route.startAddress) 
       .position(route.startLocation))); 
     destinationMarkers.add(mMap.addMarker(new MarkerOptions() 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green)) 
       .title(route.endAddress) 
       .position(route.endLocation))); 

     PolylineOptions polylineOptions = new PolylineOptions(). 
       geodesic(true). 
       color(Color.BLUE). 
       width(10); 

     for (int i = 0; i < route.points.size(); i++) 
      polylineOptions.add(route.points.get(i)); 

     polylinePaths.add(mMap.addPolyline(polylineOptions)); 
    } 
} 
+0

は、それが解決かどうかであるように計算することができますか? –

答えて

0

すべての見つかった場所の距離を計算し、最も距離が短いものが最も近い場所になります。

public double distance(double lat1, double lat2, double lon1, 
     double lon2, double el1, double el2) { 

    final int R = 6371; // Radius of the earth 

    Double latDistance = Math.toRadians(lat2 - lat1); 
    Double lonDistance = Math.toRadians(lon2 - lon1); 
    Double a = Math.sin(latDistance/2) * Math.sin(latDistance/2) 
      + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) 
      * Math.sin(lonDistance/2) * Math.sin(lonDistance/2); 
    Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
    double distance = R * c * 1000; // convert to meters 

    double height = el1 - el2; 

    distance = Math.pow(distance, 2) + Math.pow(height, 2); 

    return Math.sqrt(distance); 
} 
1

あなたが距離を計算したい場合は、この方法

private double caldistance(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); 
    } 
関連する問題