【问题标题】:Recalculate directions on Google Maps在 Google 地图上重新计算路线
【发布时间】:2015-02-28 18:20:41
【问题描述】:

我有一个简单的应用程序,它可以从用户那里获取两个地址并获取行车路线,也显示在地图上。

我的问题是:

当用户实际使用应用程序时,他/她没有按照给定的指示操作,我应该如何处理?我的意思是我应该如何检测这个事件并找到新的方向?

【问题讨论】:

  • 不能使用GPS?我认为当用户did not follow the direction时,如果你fix the end address,谷歌地图应该recalculate根据用户current location的方向。
  • 是的,我可以使用 GPS。但是我不知道如何实现这个逻辑。
  • 您是否只需要获取start addressend address 并在Google map 中获取用户的路线?
  • 是的,但是如果用户开始移动并且不按照指示进行操作怎么办。我应该给他/她一个新的前进方向。

标签: android google-maps driving-directions


【解决方案1】:

正如我们所讨论的,如果您修复了startAddressendAddress,我认为Google mapRecalculate directions 并根据GPS 上的当前位置将其提供给用户。获取路线需要设置AsyncTask获取数据表单google服务,示例代码如下:

class GetDirections extends AsyncTask<String, String, String>{
        @Override
        protected String doInBackground(String... params) {
            String startAddress =  params[0];
            startAddress = startAddress.replaceAll(" ", "%20");
            getLatLng(startAddress, false);

            String endingAddress = params[1];
            endingAddress = endingAddress.replaceAll(" ", "%20");
            getLatLng(endingAddress, true);
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            String geoUriString = "http://maps.google.com/maps?addr=" +
                    addressPos.latitude + "," +
                    addressPos.longitude + "&daddr=" +
                    finalAddressPos.latitude + "," +
                    finalAddressPos.longitude;

            Intent mapCall = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUriString));
            startActivity(mapCall);
        }
    }

    protected void getLatLng(String address, boolean setDestination) {
        String uri = "http://maps.google.com/maps/api/geocode/json?address="
                + address + "&sensor=false";

        HttpGet httpGet = new HttpGet(uri);

        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();

            InputStream stream = entity.getContent();

            int byteData;
            while ((byteData = stream.read()) != -1) {
                stringBuilder.append((char) byteData);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        double lat = 0.0, lng = 0.0;

        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
            lng = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
                    .getJSONObject("geometry").getJSONObject("location")
                    .getDouble("lng");
            lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
                    .getJSONObject("geometry").getJSONObject("location")
                    .getDouble("lat");

        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (setDestination) {
            finalAddressPos = new LatLng(lat, lng);
        } else {
            addressPos = new LatLng(lat, lng);
        }

    }

然后从editText获取start and end address并在用户click get direction button时执行AsyncTask:

public void getDirections(View view) {

        String startingAddress = et_address.getText().toString();
        String finalAddress = et_finalAddress.getText().toString();

        if ((startingAddress.equals("")) || finalAddress.equals("")) {
            Toast.makeText(this, "Enter a starting and Ending address", Toast.LENGTH_SHORT).show();
        } else {
            new GetDirections().execute(startingAddress, finalAddress);
        }
    }

您可以获取我的 Github here 的所有源代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2010-11-03
    相关资源
    最近更新 更多