2016-06-06 11 views
-1

Wie kann ich die Entfernung zwischen zwei Punkten ermitteln und die Entfernung in der Karte anzeigen? bitte hilf mir. ich versucht habe Richtung zwischen zwei Punkt zu bekommen, aber ich kann nicht zwischen ihnen bekommen Abstand .. mein Code:Wie zeigt die Entfernung zwischen der Position in der Karte?

private class ParserTask extends 
     AsyncTask<String, Integer, List<List<HashMap<String, String>>>> { 

    @Override 
    protected List<List<HashMap<String, String>>> doInBackground(
      String... jsonData) { 

     JSONObject jObject; 
     List<List<HashMap<String, String>>> routes = null; 

     try { 
      Log.e("test3", "jsonData[0] " + jsonData[0]); 


      jObject = new JSONObject(jsonData[0]); 
      PathJSONParser parser = new PathJSONParser(); 
      routes = parser.parse(jObject); 


     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return routes; 
    } 

    @Override 

    protected void onPostExecute(List<List<HashMap<String, String>>> routes) { 
     ArrayList<LatLng> points = null; 
     PolylineOptions polyLineOptions = null; 
     Log.e("test4", "routes " + routes.size()); 

     // traversing through routes 
     for (int i = 0; i < routes.size(); i++) { 
      points = new ArrayList<LatLng>(); 
      polyLineOptions = new PolylineOptions(); 
      List<HashMap<String, String>> path = routes.get(i); 

      for (int j = 0; j < path.size(); j++) { 
       HashMap<String, String> point = path.get(j); 


       double lat = Double.parseDouble(point.get("lat")); 
       double lng = Double.parseDouble(point.get("lng")); 
       LatLng position = new LatLng(lat, lng); 




       points.add(position); 


      } 


      polyLineOptions.addAll(points); 
      polyLineOptions.width(10); 
      polyLineOptions.color(Color.BLUE); 
     } 
     googleMap.addPolyline(polyLineOptions); 


    } 

} 
/*public float[] getApproxDistance(){ 
    //Location loc= new Location(mCurrentLocation); 


    //Location loc=new Location(""); 
    String origin = mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude(); 
    String destination =mDestLocation.latitude + "," + mDestLocation.longitude; 
    Double originLat= mCurrentLocation.getLatitude(); 
    Double originLong= mCurrentLocation.getLongitude(); 

    Double destLat= mDestLocation.latitude; 
    Double destLong= mDestLocation.longitude; 

    Location.distanceBetween(originLat, originLong, destLat, destLong, distance); 

    Log.e("TAG", ""+distance); 


    return distance; 

} 
*/ 
private String getMapsApiDirectionsUrl() { 


    String origin = "origin=" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude(); 
    String destination = "destination=" + mDestLocation.latitude + "," + mDestLocation.longitude; 
    /*String origin = mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude(); 
    String destination =mDestLocation.latitude + "," + mDestLocation.longitude;*/ 


    String waypoints = "waypoints=optimize:true|" 
      + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude() 
      + "|" + "|" + mDestLocation.latitude + "," 
      + mDestLocation.longitude; 
    String sensor = "sensor=false"; 
    String params = origin + "&" + destination + "&" + waypoints + "&" + sensor; 
    String output = "json"; 
    String url = "https://maps.googleapis.com/maps/api/directions/" 
      + output + "?" + params; 



    return url; 




} 
} 
+0

https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=AIzaSyAOngZ2BFuDeE2J2pwHm9d2Rv6ZnrfZ1JY –

+0

in diesem Service, den wir r immer Entfernungsparameter –

Antwort

0

Bitte verwenden - yourLocation.distanceTo (otherLocation);

wobei float distanceTo (Standort dest) Gibt die ungefähre Entfernung in Metern zwischen diesem Standort und dem angegebenen Standort an. Die Entfernung wird mit dem WGS84-Ellipsoid definiert.

Parameter Zielort: der Zielort;

Verwandte Themen