Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7863779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:35:28+00:00 2026-06-02T23:35:28+00:00

In my android app i ve a MapView, i ve to show route b/w

  • 0

In my android app i ve a MapView, i ve to show route b/w two points i implemented code but its showing only half route. i tried in many ways but could not resolve it please help where i am doing mistake.here is a screen shot two red points and blue route http://www.flickr.com/photos/77093196@N03/7117948377/
following is the code:

MapdirectionsActivity.java

public class MapdirectionsActivity extends MapActivity {
    /** Called when the activity is first created. */

    MapView mapView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.mapview);
        double src_lat = 17.3667; // the source
        double src_long = 78.4667;
        double dest_lat =  18.9647; // the destination
        double dest_long = 72.8258;
        GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),(int) (src_long * 1E6));
        GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),(int) (dest_long * 1E6));
        DrawPath(srcGeoPoint, destGeoPoint, mapView);
        mapView.getController().animateTo(srcGeoPoint);
        mapView.getController().setZoom(7);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    private void DrawPath(GeoPoint src, GeoPoint dest, MapView mMapView) {

        // connect to map web service
        StringBuilder urlString = new StringBuilder();
        urlString.append("http://maps.google.com/maps?f=d&hl=en");
        urlString.append("&saddr=");// from
        urlString.append(Double.toString((double) src.getLatitudeE6() / 1.0E6));
        urlString.append(",");
        urlString.append(Double.toString((double) src.getLongitudeE6() / 1.0E6));
        urlString.append("&daddr=");// to
        urlString.append(Double.toString((double) dest.getLatitudeE6() / 1.0E6));
        urlString.append(",");
        urlString.append(Double.toString((double) dest.getLongitudeE6() / 1.0E6));
        urlString.append("&ie=UTF8&om=1&output=kml");
        Log.d("Map directions", "URL= " + urlString.toString());

        // get the kml (XML) doc. And parse it to get the coordinates(direction route).
        Document doc = null;
        HttpURLConnection urlConnection = null;
        URL url = null;
        try {
            url = new URL(urlString.toString());
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.connect();

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(urlConnection.getInputStream());

            if (doc.getElementsByTagName("GeometryCollection").getLength() > 0) {
                String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue();
                Log.d("Map directions", "path= " + path);
                String[] pairs = path.split(" ");
                String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude
                                                        // lngLat[1]=latitude
                                                        // lngLat[2]=height
                GeoPoint startGP = new GeoPoint(
                        (int) (Double.parseDouble(lngLat[1]) * 1E6),
                        (int) (Double.parseDouble(lngLat[0]) * 1E6));

                mMapView.getOverlays().add(new MyOverLay(startGP, startGP, 1));//starting point
                GeoPoint gp1 = null;
                GeoPoint gp2 = startGP;
                for (int i = 1; i <pairs.length; i++)
                {
                    lngLat = pairs[i].split(",");
                    gp1 = gp2;
                    // watch out! For GeoPoint, first:latitude, second:longitude
                    gp2 = new GeoPoint(
                            (int) (Double.parseDouble(lngLat[1]) * 1E6),
                            (int) (Double.parseDouble(lngLat[0]) * 1E6));

                    mMapView.getOverlays().add(new MyOverLay(gp1, gp2, 2));//route
                }

                mMapView.getOverlays().add(new MyOverLay(dest, dest, 3));   //last point
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }
}

MyOverLay.java

public class MyOverLay extends Overlay {
    private GeoPoint gp1;
    private GeoPoint gp2;
    private int mRadius = 6;
    private int mode = 0;

    public MyOverLay(GeoPoint gp1, GeoPoint gp2, int mode) // Constructor
    {
        this.gp1 = gp1;
        this.gp2 = gp2;
        this.mode = mode;
    }

    public int getMode() {
        return mode;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {

        Projection projection = mapView.getProjection();
        if (shadow == false) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            Point point = new Point();
            projection.toPixels(gp1, point);
            // start
            if (mode == 1) {
                paint.setColor(Color.RED);
                RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
                        point.x + mRadius, point.y + mRadius);

                canvas.drawOval(oval, paint);// start point
            }
            // mode=2&#65306;path
            else if (mode == 2) {
                paint.setColor(Color.BLUE);
                Point point2 = new Point();
                projection.toPixels(gp2, point2);
                paint.setStrokeWidth(5);
                paint.setAlpha(120);
                canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);// mode=2&#65306;path
            }
            /* mode=3&#65306;end */
            else if (mode == 3) {
                /* the last path */

                paint.setColor(Color.RED);
                Point point2 = new Point();
                projection.toPixels(gp2, point2);
                paint.setStrokeWidth(5);
                paint.setAlpha(120);
                canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
                RectF oval = new RectF(point2.x - mRadius, point2.y - mRadius,
                        point2.x + mRadius, point2.y + mRadius);

                paint.setAlpha(255);
                canvas.drawOval(oval, paint);/* end point */
            }
        }
        return super.draw(canvas, mapView, shadow, when);
    }

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="0E2QBBM0fi1TSN6J0hCLbETO8oscApDt5CDqgbQ"
    android:clickable="true"
    android:enabled="true" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:35:29+00:00Added an answer on June 2, 2026 at 11:35 pm

    Refer this link to draw driving path in your application. You just need to add the four classes present in the link in your project and call the below lines when you need to display the route.

    SharedData data = SharedData.getInstance();
    data.setAPIKEY("0RUTLH7cqd6yrZ0FdS0NfQMO3lioiCbnH-BpNQQ");//set your map key here
    data.setSrc_lat(17);//set your src lat
    data.setSrc_lng(78);//set your src lng
    data.setDest_lat(18);//set your dest lat
    data.setDest_lng(77);//set your dest lng
    startActivity(new Intent(YourActivity.this,RoutePath.class));//add RoutePath in your manifeast file
    

    //Also add the permission to your manifeast file

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android app with a MapView, and zoom controls have been implemented.
I am trying to show Google map in android emulator but there's only gray
I am working on an Android app that utilizes the Google Maps API MapView,
My android app has a two word app name, and the 2nd word doesn't
I'm using http://developer.android.com/resources/tutorials/views/hello-mapview.html#top tutorial to show multi location in the map using markers.but it
i've been trying to create a map-related android app, but i realized that the
Say I have mapview control in my Android app. If I know a landmark
I am writing a simple Android app with an EditText and a MapView .
Hi? I am working on a MapView app in Android. I have three markers
In my android app i am displaying route b/w current_location and user selected near

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.