2016-05-15 4 views
2

Ich muss die Entfernung zwischen dem aktuellen Standort und dem Ziel berechnen. Ich habe die Breite und Länge von aktuellen und Zielorten. Ich fand den folgenden Code von SO und Internet während der Suche. Aber die Berechnung gibt 1366 km, während die Google Maps 1675 km zwischen 2 Standorten gibt. Kann jemand helfen, wie kann ich genaue Entfernung berechnen? Die Destinationen sind weltweit einschließlich meiner gegenwärtigen Stadtstandorte.Entfernung zwischen 2 Orten auf Android-Karte

//Distance in Kilometers 
    fun distanceInKms (lat1: Double, long1: Double, lat2: Double, long2: Double) : Double 
    { 
     val degToRad= Math.PI/180.0; 
     val phi1 = lat1 * degToRad; 
     val phi2 = lat2 * degToRad; 
     val lam1 = long1 * degToRad; 
     val lam2 = long2 * degToRad; 

     return 6371.01 * Math.acos(Math.sin(phi1) * Math.sin(phi2) + Math.cos(phi1) * Math.cos(phi2) * Math.cos(lam2 - lam1)); 
    } 

Kann mir bitte jemand helfen?

+0

tun Sie gerade Linie Abstand wollen, oder Sie Oberflächenabstand wollen (arc, curve)? – gian1200

+0

was meinst du mit genauer entfernung? –

Antwort

0

prüft unten Beispiel gibt genaues Ergebnis

public double CalculationByDistance(LatLng StartP, LatLng EndP) { 
     int Radius = 6371;// radius of earth in Km 
     double lat1 = StartP.latitude; 
     double lat2 = EndP.latitude; 
     double lon1 = StartP.longitude; 
     double lon2 = EndP.longitude; 
     double dLat = Math.toRadians(lat2 - lat1); 
     double dLon = Math.toRadians(lon2 - lon1); 
     double a = Math.sin(dLat/2) * Math.sin(dLat/2) 
       + Math.cos(Math.toRadians(lat1)) 
       * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon/2) 
       * Math.sin(dLon/2); 
     double c = 2 * Math.asin(Math.sqrt(a)); 
     double valueResult = Radius * c; 
     double km = valueResult/1; 
     DecimalFormat newFormat = new DecimalFormat("####"); 
     int kmInDec = Integer.valueOf(newFormat.format(km)); 
     double meter = valueResult % 1000; 
     int meterInDec = Integer.valueOf(newFormat.format(meter)); 
     Log.i("Radius Value", "" + valueResult + " KM " + kmInDec 
       + " Meter " + meterInDec); 

     return Radius * c; 
    } 
+0

Ist die Entfernung in km? – User

+0

ja Entfernung in km – Krishna

+0

Nein. Die Abv-Berechnung ergibt 1366. lat und lang für beide sind -37,5931551,144,7216202, -27,485572,153,038064. Berechnen und sehen Sie – User

1

Verwenden android.location.Location Klasse, verfügbar seit API-Level 1 in Android. Es hat eine statische distanceBetween Methode, die alles für Sie tut.

Siehe: http://developer.android.com/reference/android/location/Location.html

float[] results = new float[1]; 
android.location.Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results); 
    //distance in meters now in results[0] 

Division durch 1000 in Kilometern (km) zu erhalten.

+0

Ich benutzte distanceTo aber das gleiche Problem – User

+0

Verwenden Sie vielleicht nicht-Dezimal-Grad-Format? Weil das mit Sicherheit richtig ist. –

+0

Oder wie genau berechnen Sie es mit Google Maps? Straße/Auto Entfernung vielleicht. –

0

Sie es verwenden können, um es wie Google vergessen nicht den Internet-Erlaubnis

 String getDistanceOnRoad(double latitude, double longitude, 
      double prelatitute, double prelongitude) { 
     String result_in_kms = ""; 
     String url = "http://maps.google.com/maps/api/directions/xml?    origin=" 
       + latitude + "," + longitude + "&destination=" + prelatitute 
       + "," + prelongitude + "&mode=driving&sensor=false&units=metric"; 
     String tag[] = { "text" }; 
     HttpResponse response = null; 
     try { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpContext localContext = new BasicHttpContext(); 
      HttpPost httpPost = new HttpPost(url); 
      response = httpClient.execute(httpPost, localContext); 
      InputStream is = response.getEntity().getContent(); 
      DocumentBuilder builder = DocumentBuilderFactory.newInstance() 
        .newDocumentBuilder(); 
      Document doc = builder.parse(is); 
      if (doc != null) { 
       NodeList nl; 
       ArrayList<String> args = new ArrayList<String>(); 
       for (String s : tag) { 
        nl = doc.getElementsByTagName(s); 
        if (nl.getLength() > 0) { 
         Node node = nl.item(nl.getLength() - 1); 
         args.add(node.getTextContent()); 
        } else { 
         args.add(" - "); 
        } 
       } 
       result_in_kms = String.format("%s", args.get(0)); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return result_in_kms; 
    } 

diese Bibliothek Klasse hinzufügen Zeit, Distanz und ziehen Linienzug zwischen zwei Orten es wie google map arbeiten zu bekommen https://github.com/memo1231014/MUT-master

Beispiel für die Bibliothek

RouteInformations rInformation = new RouteInformations(new  AsyncResponse() { 

      @Override 
      public void processFinish(RouteDetails arg0) { 
       // TODO Auto-generated method stub 
       try 
       { 
        map.addPolyline(arg0.getLineOptions()); //you can add the return line and add it to the map 

        //here you can get distance , duration it will return like you drive a car 
        MUT.fastDialog(Map.this,"Time and Distance","Distance : "+arg0.getDistance()+"\nDuration : "+arg0.getDuration()); 
       } 
       catch(Exception e) 
       { 
        MUT.lToast(Map.this,"Can't draw line Try Again"); 
       } 
       } 
     }); 

     //you should pass the 2 lat and lang which you want to draw a aline or get distance or duration between them 
     RouteDetails routeDetails=new RouteDetails(); 
     routeDetails.setLatLong1(from.getPosition()); 
     routeDetails.setLatLong2(to.getPosition()); 
     rInformation.execute(routeDetails);