Android的谷歌地图的发现距离 [英] android google map finding distance

查看:89
本文介绍了Android的谷歌地图的发现距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到两个位置之间的距离。我有经度和纬度,我可以计算出欧氏距离。但我想找到路的距离。我的意思是,我想计算,我怎么回事,当去到目的地,从源头路的距离。在这种情况下,如何计算呢?

I am trying to find distance between two locations. I have longitudes and latitudes and I can calculate Euclidean distance. But I want to find road distance. I mean, , I want to calculate the distance of the road that I am going on while going to destination from source. In this case how to calculate this?

推荐答案

最简单的方法是使用谷歌路线API来获取方向,这给你沿线所有点的列表(和总距离)。

The easiest way would be to use the Google Directions API to get the directions, this gives you a list of all the points along the route (and the total distance).

查看:<一href="http://$c$c.google.com/apis/maps/documentation/directions/">http://$c$c.google.com/apis/maps/documentation/directions/

如果你不知道如何做到这一点,让我知道,我会发布一些$ C $下您将得到谷歌的方向和提取所需的数据。

If your not sure how to do this let me know and i'll post some code for you which will get the directions from google and extract the data you need.

更新 - code的要求

UPDATE - CODE AS REQUESTED

private void GetDistance(GeoPoint src, GeoPoint dest) {

    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.googleapis.com/maps/api/directions/json?");
    urlString.append("origin=");//from
    urlString.append( Double.toString((double)src.getLatitudeE6() / 1E6));
    urlString.append(",");
    urlString.append( Double.toString((double)src.getLongitudeE6() / 1E6));
    urlString.append("&destination=");//to
    urlString.append( Double.toString((double)dest.getLatitudeE6() / 1E6));
    urlString.append(",");
    urlString.append( Double.toString((double)dest.getLongitudeE6() / 1E6));
    urlString.append("&mode=walking&sensor=true");
    Log.d("xxx","URL="+urlString.toString());

    // get the JSON And parse it to get the directions data.
    HttpURLConnection urlConnection= null;
    URL url = null;

    url = new URL(urlString.toString());
    urlConnection=(HttpURLConnection)url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.connect();

    InputStream inStream = urlConnection.getInputStream();
    BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));

    String temp, response = "";
    while((temp = bReader.readLine()) != null){
        //Parse data
        response += temp;
    }
    //Close the reader, stream & connection
    bReader.close();
    inStream.close();
    urlConnection.disconnect();

    //Sortout JSONresponse 
    JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
    JSONArray array = object.getJSONArray("routes");
        //Log.d("JSON","array: "+array.toString());

    //Routes is a combination of objects and arrays
    JSONObject routes = array.getJSONObject(0);
        //Log.d("JSON","routes: "+routes.toString());

    String summary = routes.getString("summary");
        //Log.d("JSON","summary: "+summary);

    JSONArray legs = routes.getJSONArray("legs");
        //Log.d("JSON","legs: "+legs.toString());

    JSONObject steps = legs.getJSONObject(0);
            //Log.d("JSON","steps: "+steps.toString());

    JSONObject distance = steps.getJSONObject("distance");
        //Log.d("JSON","distance: "+distance.toString());

            String sDistance = distance.getString("text");
            int iDistance = distance.getInt("value");

}

这将返回两个点之间的距离,可以改变功能,它返回一个字符串或一个整数,只是返回无论是sDistance或iDistance。

This will return the distance between the two points, you can change the function to return it as a string or as an integer, just return either sDistance or iDistance.

希望这可以帮助你,让我知道如果你需要更多的帮助。

Hope this helps you, let me know if you need any more help.

这篇关于Android的谷歌地图的发现距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆