2

I have a simple application which can get two address from the user and get a driving direction, shows on the map too.

My question is:

How should I handle the case when the user actually uses the app, and he/she doesn't follow the given directions? I mean how should I detect this event and get a new direction?

erbal
  • 727
  • 3
  • 12
  • 32
  • Can not use the `GPS`? I think when the user `did not follow the direction`, the google map should `recalculate` the directions according to `current location` of the user if you `fix the end address`. – bjiang Feb 28 '15 at 18:27
  • Yes, I can use the GPS. But I don't know how to implement this logic. – erbal Feb 28 '15 at 18:29
  • Do you just need to get the `start address` and `end address` and get the directions for the user in the `Google map`? – bjiang Feb 28 '15 at 18:30
  • Yes, but what if the user starts to move and doesn't follow the directions. I should give him/her a new direction to go. – erbal Feb 28 '15 at 18:44

1 Answers1

0

As we discussed, I think the Google map will Recalculate directions and give it to users base on current location on GPS if you fixed the startAddress and endAddress. For get the directions, you need to set up a AsyncTask to get the data form google service, sample code as follows:

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);
        }

    }

Then get the start and end address form the editText and execute the AsyncTask when user click get direction button:

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);
        }
    }

You can get all source code for my Github here.

enter image description here

bjiang
  • 6,068
  • 2
  • 22
  • 35