使用谷歌功能使用纬度和经度计算距离

时间:2014-04-24 09:38:57

标签: android google-maps

我在做什么 :: 我试图用源和destionation的纬度和经度坐标来计算android中两点之间的距离


发生了什么 :: 我正在获取日志中显示的输出


Coordinates i am using

  • SourceLatitude :: 12.918286
  • SourceLongitude :: 77.669493
  • DestinationLatitude :: 12.959926
  • DestinationLongitude :: 77.647614

当我在Google地图中查看时,我获得距离 = 10.6 km


问题::

  • 当我编译下面的代码时,我得到了Log,如我所示 获得值5182.4204
  • 为什么会这样?
  • 如何将价值转换为kilometers

MainActivity.java

public class MainActivity extends Activity {

    public static String srcLatitude="12.918286";
    public static String srcLongitude="77.669493";
    public static String destLatitude="12.959926";
    public static String destLongitude="77.647614";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("- Destination -", destLatitude+","+destLongitude);
        Log.d("- Source -", srcLatitude+","+srcLongitude);

        float CalculateDistance=CalculateDistance(srcLatitude,srcLongitude,destLatitude,destLongitude);

        Log.d("- Result -", CalculateDistance+"");


    }

    private float CalculateDistance(String srcLatitude,
            String srcLongitude, String destLatitude, String destLongitude) {

        Location locationA = new Location("point A");

        //Convert from string to double and then process
        locationA.setLatitude(Double.parseDouble(destLatitude));
        locationA.setLongitude(Double.parseDouble(destLongitude));

        Location locationB = new Location("point B");

        locationB.setLatitude(Double.parseDouble(srcLatitude));
        locationB.setLongitude(Double.parseDouble(srcLongitude));

        return locationA.distanceTo(locationB);
    }


}

日志 ::

04-24 15:06:57.674: D/dalvikvm(588): Not late-enabling CheckJNI (already on)
04-24 15:07:00.143: D/- Destination -(588): 12.959926,77.647614
04-24 15:07:00.143: D/- Source -(588): 12.918286,77.669493
04-24 15:07:00.193: D/- Result -(588): 5182.4204
04-24 15:07:00.563: D/gralloc_goldfish(588): Emulator without GPU emulation detected.

{编辑}

package com.example.latitudelongitudegoogleway;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    public static String srcLatitude="12.918286";
    public static String srcLongitude="77.669493";
    public static String destLatitude="12.959926";
    public static String destLongitude="77.647614";

    public static double hsrcLatitude=12.918286;
    public static double hsrcLongitude=77.669493;
    public static double hdestLatitude=12.959926;
    public static double hdestLongitude=77.647614;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("- Destination -", destLatitude+","+destLongitude);
        Log.d("- Source -", srcLatitude+","+srcLongitude);

        float CalculateDistance=CalculateDistance(srcLatitude,srcLongitude,destLatitude,destLongitude);

        Log.d("- Result -", CalculateDistance+"");

        double hCalculateDistance=distFrom(hsrcLatitude,hsrcLongitude,hdestLatitude,hdestLongitude);

        Log.d("- Result -", hCalculateDistance+"");
    }

    private float CalculateDistance(String srcLatitude,
            String srcLongitude, String destLatitude, String destLongitude) {

        Location locationA = new Location("point A");

        //Convert from string to double and then process
        locationA.setLatitude(Double.parseDouble(destLatitude));
        locationA.setLongitude(Double.parseDouble(destLongitude));

        Location locationB = new Location("point B");

        locationB.setLatitude(Double.parseDouble(srcLatitude));
        locationB.setLongitude(Double.parseDouble(srcLongitude));

        return locationA.distanceTo(locationB)/1000;
    }


    public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
        double earthRadius = 3958.75;
        double dLat = Math.toRadians(lat2-lat1);
        double dLng = Math.toRadians(lng2-lng1);
        double sindLat = Math.sin(dLat / 2);
        double sindLng = Math.sin(dLng / 2);
        double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
                * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double dist = earthRadius * c;

        return dist/1000;
        }
}

日志 ::

04-24 15:37:43.113: D/- Source -(851): 12.918286,77.669493
04-24 15:37:43.133: D/- Result -(851): 5.1824203
04-24 15:37:43.133: D/- Result -(851): 3.2323368667198737
04-24 15:37:43.353: D/gralloc_goldfish(851): Emulator without GPU emulation detected.

1 个答案:

答案 0 :(得分:0)

您是如何“检查”Google地图中的直接距离的?按标准显示它可以显示道路距离,而不是直接距离......

尝试使用Haversine公式仔细检查distanceTo()函数的结果: