【问题标题】:Calculating Distance using latitude and Longitude using google functions使用谷歌函数使用纬度和经度计算距离
【发布时间】:2014-04-24 09:38:56
【问题描述】:

我在做什么:: 我正在尝试使用源和目标的纬度和经度坐标计算android中两点之间的距离


发生了什么:: 我得到了日志中显示的输出


Coordinates i am using

  • SourceLatitude:: 12.918286
  • 来源经度:: 77.669493
  • 目的地纬度::12.959926
  • 目的地经度:: 77.647614

当我在谷歌地图中查看时,我得到了距离= 10.6 km


问题 ::

  • 当我编译下面的代码时,我得到了清晰显示的日志,我是 获取值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.

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

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

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

    【讨论】:

    • 我尝试了第二个选项..........它给了我 3 公里....而我上面显示给了我 5 公里(从米转换为公里后).... . 谷歌地图显示10公里怎么会有这样的差异
    • 第二个返回里程!将 earthRadius 改为 6371,见en.wikipedia.org/wiki/Earth_radius
    • @ Tobi ...... 它是 5.201 ........ 所以 & hervashine 之间的距离返回相同的距离但是为什么当我在谷歌验证时谷歌地图显示 10 ........是因为路途距离吗?
    • 看看谷歌地图。你可以看到它给你的不是直接距离,而是道路距离!
    猜你喜欢
    • 2011-05-18
    • 2013-04-16
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2021-09-05
    • 2021-10-18
    相关资源
    最近更新 更多