Search code examples
androidgoogle-mapsgoogle-directions-api

how to Highlight the road 1Km Ahead and 1km back using a single coordinate


Let's say i have a point that exists on the road. Now i want to highlight that road using that point for about 1-1.5 Kilometer as highlighted in image below

enter image description here

any solution how to do this??

Thanks in Advance.


Solution

  • Here is the code. Hope it helps;

       public class SnapToRoad extends AsyncTask<Void, Void, Void> {
    
    private static final String TAG = SnapToRoad.class.getSimpleName();
    
    @Override
    protected Void doInBackground(Void... params) {
        Reader rd = null;
        try {
            URL url = new URL("http://maps.google.com/maps/api/directions/xml?origin=52.0,0&destination=52.0,0&sensor=true");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setReadTimeout(10000 /* milliseconds */);
            con.setConnectTimeout(15000 /* milliseconds */);
            con.connect();
            if (con.getResponseCode() == 200) {
    
                rd = new InputStreamReader(con.getInputStream());
                StringBuffer sb = new StringBuffer();
                final char[] buf = new char[1024];
                int read;
                while ((read = rd.read(buf)) > 0) {
                    sb.append(buf, 0, read);
                }
                Log.v(TAG, sb.toString());
            } 
            con.disconnect();
        } catch (Exception e) {
            Log.e("foo", "bar", e);
        } finally {
            if (rd != null) {
                try {
                    rd.close();
                } catch (IOException e) {
                    Log.e(TAG, "", e);
                }
            }
        }
        return null;
    }