android - 如何在不使用谷歌地图方向 api 的情况下查找 Android 应用程序中 2 个地理点之间的距离(通过公路)?

标签 android google-maps

我正在开发 Android 应用程序。我需要找到两个地理坐标之间的距离。

我使用了 Location.distanceBetween()Location.distanceTo() 函数。这些函数给出了直线距离,但是当我们走公路时,实际距离是不同的。

最佳答案

使用 google api 作为

public float getDistance(double lat1, double lon1, double lat2, double lon2) {
        String result_in_kms = "";
        String url = "http://maps.google.com/maps/api/directions/xml?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&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 args = new ArrayList();
                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.valueOf( args.get(0));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Float f=Float.valueOf(result_in_kms);
        return f*1000;
    }

或者你可以使用下面的函数

public final static double AVERAGE_RADIUS_OF_EARTH = 6371;
    public float calculateDistance(double userLat, double userLng, double venueLat, double venueLng) {

        double latDistance = Math.toRadians(userLat - venueLat);
        double lngDistance = Math.toRadians(userLng - venueLng);

        double a = (Math.sin(latDistance / 2) * Math.sin(latDistance / 2)) +
                        (Math.cos(Math.toRadians(userLat))) *
                        (Math.cos(Math.toRadians(venueLat))) *
                        (Math.sin(lngDistance / 2)) *
                        (Math.sin(lngDistance / 2));

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        return (float) (Math.round(AVERAGE_RADIUS_OF_EARTH * c));

    }

关于android - 如何在不使用谷歌地图方向 api 的情况下查找 Android 应用程序中 2 个地理点之间的距离(通过公路)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22609087/

相关文章:

javascript - 谷歌地图后面的整页背景图像不会出现在 html 输出中

google-maps - Google Maps JavaScript API Heatmap Layer-可以在 map 上显示的数据点的限制?

Android - 菜单项无法解析为 onOptionsItemSelected() 中的类型

android - WebSocket 在几个时间间隔之间不断断开连接

android - 以编程方式更改 AppBarLayout 主题?

android - OpenOptionsMenu 不适用于 ActionBarSherlock 自定义子菜单

androidx 数据绑定(bind)错误 : package android. support.constraint 不存在

javascript - 谷歌地图集群自定义图像

javascript - Google map (gmap3 插件),点击时淡出叠加

java - NullPointerException - Android 谷歌地图街景