【问题标题】:Google Map API v2 - Get Driving Distance from Current Location to Known LocationsGoogle Map API v2 - 获取从当前位置到已知位置的行驶距离
【发布时间】:2013-07-26 12:00:24
【问题描述】:

我试图弄清楚如何显示从当前位置到四个已知位置的行驶距离。我没有在屏幕上显示地图,我只需要显示用户当前距离这些位置有多少英里。我从Here 中举了一个例子,稍微修改了一下。它带回了正确的距离,但我不知道如何通过 4 个终点位置中的每一个,或区分它们。

我需要做什么才能获得到 4 个位置的距离并在单独的 TextView 中显示这些距离?

更新问题:如何分别计算 4 个 TextView 的距离?

使用下面的代码,我可以为第一个 TextView 做我想做的事情

Java

public class Locations extends Fragment {

private Location currentLocation = null;
private LocationManager locationManager;
private GeoPoint currentPoint;

TextView location1;

ArrayList<LatLng> markerPoints;
GoogleMap map;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getLastLocation();
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_locations, container, false);
    ...some other stuff being done here...
    // Return view
    return view;
}

public void getLastLocation(){
    String provider = getBestProvider();
    currentLocation = locationManager.getLastKnownLocation(provider);

    this.markerPoints = new ArrayList<LatLng>();

    LatLng fromPosition = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
    LatLng toPosition = new LatLng(29.633289, -82.305838);
    // These are the other 3 end locations
    LatLng toPosition1 = new LatLng(35.205374, -82.614587);
    LatLng toPosition2 = new LatLng(35.405342, -82.316587);
    LatLng toPosition3 = new LatLng(35.702354, -82.515837);

    Locations.this.markerPoints.add(fromPosition);
    Locations.this.markerPoints.add(toPosition);

    // Getting URL to the Google Directions API
    String url = Locations.this.getDirectionsUrl(fromPosition, toPosition);

    DownloadTask downloadTask = new DownloadTask();

    // Start downloading json data from Google Directions API
    downloadTask.execute(url);

    if(currentLocation != null) {
        setCurrentLocation(currentLocation);
    } else { 
        // do something
    }
}

public String getBestProvider() {
    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    criteria.setAccuracy(Criteria.NO_REQUIREMENT);
    String bestProvider = locationManager.getBestProvider(criteria, true);
    return bestProvider;
}

public void setCurrentLocation(Location location){
    // Get current location
    int currLatitude = (int) (location.getLatitude()*1E6);
    int currLongitude = (int) (location.getLongitude()*1E6);
    currentPoint = new GeoPoint(currLatitude,currLongitude); 
    // Set current location
    currentLocation = new Location("");
    currentLocation.setLatitude(currentPoint.getLatitudeE6() / 1e6);
    currentLocation.setLongitude(currentPoint.getLongitudeE6() / 1e6);
}

private String getDirectionsUrl(LatLng origin, LatLng dest) {
    // Origin of route
    String str_origin = "origin=" + origin.latitude + "," + origin.longitude;
    // Destination of route
    String str_dest = "destination=" + dest.latitude + "," + dest.longitude;
    // Sensor enabled
    String sensor = "sensor=false";
    // Building the parameters to the web service
    String parameters = str_origin + "&" + str_dest + "&" + sensor;
    // Output format
    String output = "json";
    // Building the url to the web service
    String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;

    return url;
}

/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException {
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;
    try
    {
        URL url = new URL(strUrl);
        // Creating an http connection to communicate with url
        urlConnection = (HttpURLConnection) url.openConnection();
        // Connecting to url
        urlConnection.connect();
        // Reading data from url
        iStream = urlConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
        StringBuffer sb = new StringBuffer();
        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        data = sb.toString();
        br.close();
    } catch (Exception e) {
        Log.d("Exception while downloading url", e.toString());
    } finally {
        iStream.close();
        urlConnection.disconnect();
    }
    return data;
}

// Fetches data from url passed
private class DownloadTask extends AsyncTask<String, Void, ArrayList<String>> {
    @Override
    protected ArrayList<String> doInBackground(String... urlList) {
        try {
            ArrayList<String> returnList = new ArrayList<String>();
            for (String url : urlList) {
                // Fetching the data from web service
                String data = Locations.this.downloadUrl(url);
                returnList.add(data);
            }
            return returnList;
        } catch (Exception e) {
            Log.d("Background Task", e.toString());
            return null; // Failed, return null
        }
    }

    // Executes in UI thread, after the execution of
    // doInBackground()
    @Override
    protected void onPostExecute(ArrayList<String> results) {
        super.onPostExecute(results);

        ParserTask parserTask = new ParserTask();

        for (String url : results) {
            parserTask.execute(url);
        }

        // Invokes the thread for parsing the JSON data
        // parserTask.execute(results);
    }
}

/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, ArrayList<List<HashMap<String, String>>>> {
    // Parsing the data in non-ui thread
    @Override
    protected ArrayList<List<HashMap<String, String>>> doInBackground(String... jsonData) {
        try {
            ArrayList<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String, String>>>();

            // for (String url : jsonData) {
            for (int i = 0; i < jsonData.length; i++) {
                JSONObject jObject = new JSONObject(jsonData[i]);

                DirectionsJSONParser parser = new DirectionsJSONParser();
                // Starts parsing data
                routes = (ArrayList<List<HashMap<String, String>>>) parser.parse(jObject);
            }
            return routes;
        } catch (Exception e) {
            Log.d("Background task", e.toString());
            return null; // Failed, return null
        }
    }

    // Executes in UI thread, after the parsing process
    @Override
    protected void onPostExecute(ArrayList<List<HashMap<String, String>>> result) {
        if (result.size() < 1) {
            Toast.makeText(getActivity(), "No Points", Toast.LENGTH_LONG).show();
            return;
        }

        TextView tv1 = (TextView) getActivity().findViewById(R.id.location1);
        TextView tv2 = (TextView) getActivity().findViewById(R.id.location2);
        TextView tv3 = (TextView) getActivity().findViewById(R.id.location3);
        TextView tv4 = (TextView) getActivity().findViewById(R.id.location4);

        TextView[] views = { tv1, tv2, tv3, tv4 };

        // Traversing through all the routes
        for (int i = 0; i < result.size(); i++) {
            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);
            String distance = "No distance";

            // Fetching all the points in i-th route
            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);

                if (j == 0) {
                    distance = point.get("distance");
                    continue;
                }
            }

            Log.d("Distance: ", distance);

            // Set text
            views[i].setText(distance);
        }
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/location1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/location2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/location3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/location4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="TextView" />
</LinearLayout>
</RelativeLayout>

我们将不胜感激任何解决此问题的帮助和/或改进此代码的建议。

【问题讨论】:

  • 您是否尝试过分别计算所有 4 条路线,或者使用与 R.id.location1 不同的 TextView
  • 我已经分别尝试了它们中的每一个,例如,传入“toPosition1”并将 TextView 设置在同一位置(Java 文件中的最后一行)和 R.id.location2 和它返回正确的距离,但我一次只能得到一个正确的距离。如果我同时传递所有这些,它会为 4 个 TextView 中的每一个设置“距离”。 (希望这是有道理的......)
  • 所以您需要做的是创建一个TextView 数组,最后在您获得距离的地方循环遍历该数组,以便将每个距离设置为不同的TextView
  • 您介意在答案中发布一个我如何做到这一点的示例吗?
  • 我在下面发布了我的答案

标签: android google-maps distance google-maps-android-api-2 google-maps-api-2


【解决方案1】:

以下是您现有代码的更新:

// Executes in UI thread, after the parsing process
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> result)  
{
    if (result.size() < 1) 
    {
        Toast.makeText(getActivity(), "No Points", Toast.LENGTH_SHORT).show();
        return;
    }

    TextView tv1 = (TextView) getActivity().findViewById(R.id.location1);
    TextView tv2 = (TextView) getActivity().findViewById(R.id.location2);
    TextView tv3 = (TextView) getActivity().findViewById(R.id.location3);
    TextView tv4 = (TextView) getActivity().findViewById(R.id.location4);

    TextView[] views = {tv1, tv2, tv3, tv4};


    // Traversing through all the routes
    for (int i = 0; i < result.size(); i++) 
    {
        // Fetching i-th route
        List<HashMap<String, String>> path = result.get(i);
        String distance = "No distance";

        // Fetching all the points in i-th route
        for (int j = 0; j < path.size(); j++) 
        {
            HashMap<String, String> point = path.get(j);

            if (j == 0)  
            {
                distance = point.get("distance");
                continue;
            }
        }

        // Set text
        views[i].setText(distance);
    }
}

这段代码做了一个不太好的假设:它假设result的大小与views的大小相同,在你的情况下应该是4。当你运行这段代码时,你可能会得到一个IndexOutOfBounds 错误,如果您有超过 4 个结果(这不应该发生)。最终,您将要验证 result 的大小是 4,或者您拥有的 TextView 的数量。如果您有任何问题或这不起作用,请告诉我:)

编辑:要一次获取所有距离,您可以修改 DownloadTask 以接受多个 URL。

更改类定义:

private class DownloadTask extends AsyncTask<String, Void, ArrayList<String>>

这表示您的后台操作将返回String 的列表。

修改doInBackground(),现在可以处理多个URL:

// Downloading data in non-ui thread
@Override
protected ArrayList<String> doInBackground(String... urlList) 
{
    try 
    {
        ArrayList<String> returnList = new ArrayList<String>();
        for(String url : urlList)
        {
            // Fetching the data from web service
            String data = Locations.this.downloadUrl(url);
            returnList.add(data);
        }

        return returnList;
    } 
    catch (Exception e) 
    {
        Log.d("Background Task", e.toString());
        return null; // Failed, return null
    }
}

那你onPostExecute()就变成了

// Executes in UI thread, after the execution of
// doInBackground()
@Override
protected void onPostExecute(ArrayList<String> results) 
{
    super.onPostExecute(results);

    ParserTask parserTask = new ParserTask();

    // Invokes the thread for parsing the JSON data
    parserTask.execute(results);

}

现在,您必须修改 ParserTask 代码以接收 JSON 字符串列表,而不仅仅是一个 JSON 字符串。只需更改您的 ParserTask 输入参数并将所有内容放在 for 循环中以循环遍历每个 JSON 字符串。您还必须修改onPostExecute() 的参数以接收已经存在的List,这样它就不会处理一个结果,而是一个结果列表。我不能在这里向你展示这些修改,因为它会太长,然后你就不会有挑战了:)

编辑二:getLastLocation() 中,您只使用一个URL 调用DownloadTask,但您应该输入四个像downloadTask.execute(url1, url2, url3, url4) 这样的URL。此外,由于ParserTask 仍然只处理一个 JSON 字符串,您应该取出四个TextView 和循环出onPostExecute() 的数组。要告诉ParserTask 要填充哪个TextView,请向ParserTask 添加一个构造函数,该构造函数将TextView 作为参数。然后在ParserTask中创建一个实例变量,在构造函数中赋值并在onPostExecute()中使用来显示距离。

然后,把我之前给你的TextView数组的东西放到DownloadTaskonPostExecute()中。当您遍历 String 结果时,还要遍历 TextView 数组并在 ParserTask 构造函数中传入 TextView

基本上,您在ParserTask 中添加一个构造函数来告诉它使用哪个TextView。当您的DownloadTask 完成后,您将其传递给正确的TextView 作为URL。例如,R.id.location3 表示第三个 URL。

【讨论】:

  • 这行得通,但是我如何为其他 3 个结束位置(同时)传递 LatLng?
  • 如果我添加另一个“String url1 = Locations.this.getDirectiosnUrl(fromPosition, toPosition1);”并尝试执行“url”和“url1”,LogCat 显示非法状态异常。无法执行任务:任务已经在运行。我似乎无法弄清楚如何同时获得到所有 4 个终点位置的距离。
  • 谢谢你没有做这一切!有了您的解释,我应该能够进行剩余的更改,并且在此过程中,学习和理解如何和为什么。一旦我完成并使其正常工作,我将标记为答案!再次感谢!
  • 我用我的更改从 DownloadTask 开始更新了我的 OP。我仍然只能填充一个 TextView 但可以在 LogCat 中看到正在返回的 4 个距离中的每一个都正确。但是,当我在我的设备上运行它时,“R.id.location1”是唯一填充的 TextView,它显示了“R.id.location4”应该显示的距离。有什么想法吗?
【解决方案2】:
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;
}

此方法将计算从一个位置到另一个位置的距离,精度取决于 lat,lng 的频率。更多纬度,纬度更准确。 请记住,距离和位移存在差异。

【讨论】:

  • 这不是给我“飞越”距离,而不是实际行驶距离吗?
  • 这取决于你是直走,那么它可以给出几乎准确的结果,你可以使用更多的 lat lng 来获得准确的结果......这就是我写距离和位移的原因......
  • 那是我最初使用的公式,但随着到终点位置的距离增加,实际行驶距离与我想要完成的目标相差太大。我很抱歉在我回复时没有考虑到您提到的差异。
  • 假设如果你旅行了 10 公里并且你有 10 lat,lng 然后首先计算 1 和 2 lat 依此类推。这样你可以计算得更准确。如果可能的话,将 lat,lng 增加 10 到 20 会更准确,但资源消耗和处理速度会变慢..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
相关资源
最近更新 更多