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

  • SEARCH
  • Home
  • 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 7906141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:47:37+00:00 2026-06-03T10:47:37+00:00

In my android app i am displaying route b/w current_location and user selected near

  • 0

In my android app i am displaying route b/w current_location and user selected near theater by clicking “Get directions” Button but when user select another theater new route also displaying but old route is not clearing.i tried in several ways but i am unable to do. i tried with mapOverlays.clear(); which clearing entire map which i dont want.even i tried {mapOverlays.remove(ol1); mapView.postInvalidate();}its not clearing route please help me:following is the code:

MyOverLay.java

public class MyOverLay extends Overlay 
{ 
    private GeoPoint gp1; 
    private GeoPoint gp2; 
    private int mode=0; 
    Context mContext;

    public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode) 
    { 
        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(); 
        Log.d("shadow", ""+shadow);
        if (shadow== false) 
        { 
            Paint paint = new Paint(); 
            paint.setAntiAlias(true); 
            Point point = new Point(); 
            projection.toPixels(gp1, point); 
            if(mode==1) // start point
            { 
                    paint.setColor(Color.BLUE); 
            } 
            else if(mode==2) // mode=2,path 
            { 
                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); 
            } 
            else if(mode==3)   /* mode=3:end */ 
            { 
                /* the last path */ 
                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); 
                /* end point */
            } 
        } 
        return super.draw(canvas, mapView, shadow, when); 
    } 
}

HelloItemizedOverlay.java

public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
    WebViewDemo viewDemo;
    GeoPoint curret_point;

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    Context mContext;

   //some methods

    public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
        viewDemo=(WebViewDemo)context;
    }

    //When user taps theater icon this alert box pop ups.which contains 'Get directions' Button
    @Override
    protected boolean onTap(int index) { 
         WebViewDemo.MyOverLayItem item = (WebViewDemo.MyOverLayItem) mOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        viewDemo.desc_lat=item.getPoint().getLatitudeE6();
        viewDemo.desc_lng=item.getPoint().getLongitudeE6();
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.setPositiveButton("Get directions", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                viewDemo.showDrections();
            }
        });
        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return true;
    }
}

Main part of WebViewDemo Map Activity:

WebViewDemo.java

public class WebViewDemo extends MapActivity {

public double current_latitude, current_langitude;
Overlay ol1;
MapView mapView;
GeoPoint destGeoPoint;
HttpClient httpClient;
HttpPost postRequest;
HttpResponse response;
BufferedReader reader;
String sResponse, result;
StringBuilder s;
JSONObject jsonObject;
public String lat = null, lng = null;
int desc_lat,desc_lng;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
.....
mapView = (MapView) findViewById(R.id.mapview);
mapOverlays = mapView.getOverlays();
/**Here i am getting current_latitude, current_langitude using Location Manager
and displaying it on map.and making Web Service request to google and displaying near theaters also.*/
..
}//onCreate end

// *For displaying route b/w current location and User selected Theaters when Get directions Button clicked (in HelloItemizedOverlay class). 

public void showDrections() {

        try {
                      //Here i am trying  to clear previous path. 
            if(ol1!=null){
                mapOverlays.remove(ol1);
                mapView.postInvalidate();
                mapView.invalidate();
            }
            double l1 = desc_lat/1E6;
            double l2 = desc_lng/1E6;
            destGeoPoint = new GeoPoint(desc_lat,desc_lng);
            httpClient = new DefaultHttpClient();
            postRequest = new HttpPost(
                    "http://maps.googleapis.com/maps/api/directions/json?origin="
                            + current_latitude
                            + ","
                            + current_langitude
                            + "&destination="
                            + l1
                            + ","
                            + l2
                            + "&sensor=true&optimize=true&alternatives=false");
            response = httpClient.execute(postRequest);
            reader = new BufferedReader(new InputStreamReader(response
                    .getEntity().getContent(), "UTF-8"));// getting response
            sResponse = null;
            s = new StringBuilder();
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
            result = s.toString().trim();
            jsonObject = new JSONObject(result);

            JSONArray jsonRouteArray = jsonObject.getJSONArray("routes");
            JSONObject overview_poly = jsonRouteArray.getJSONObject(0).getJSONObject("overview_polyline");
            String points = overview_poly.getString("points");
            List<GeoPoint> _geopoints = decodePoly(points);
            GeoPoint gp1;
            GeoPoint gp2;
            gp2 = _geopoints.get(0);
            for (int i = 1; i < _geopoints.size(); i++) // the last one  would be crash
            {
                gp1 = gp2;
                gp2 = _geopoints.get(i);
                ol1 = new MyOverLay(gp1, gp2, 2);
                mapOverlays.add(ol1);
            }
            mapView.postInvalidate();
            dialog.cancel();
        } catch (JSONException je) {
            Toast.makeText(this, "Unable to Show The Route", Toast.LENGTH_LONG).show();
            Log.d("showDirectins() JSON ERROR", ""+je);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
private List<GeoPoint> decodePoly(String encoded) {
//this method is used to decode the route points which are coming from web service and convert them into GeoPoints
}

}

Please Suggest me code changes if required.

  • 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-03T10:47:44+00:00Added an answer on June 3, 2026 at 10:47 am

    there is a possibility to remove route in map, please store all overlays references to one List while creating route , when you want to remove iterate list remove all overlay references from mapview. I hope it will work.

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

Sidebar

Related Questions

My Android app is retreiving the user's friends list and displaying it to them
I'm making an app for android, I'm using Google Directions API to get and
I have an Android app that begins by displaying a dialog. When I set
I'm having problems properly displaying an alert in my Android app. The goal is
I am developing a gps tracking app in android. I am done with displaying
I'm having trouble displaying SOME images on my android app. Just normal display image
My Android app is displaying text in a TextView. Are there any tags or
I'm looking at displaying a map within my Android app that presents maritime charts,
I have created a splashscreen for my android app. I went down the route
Possible Duplicate: Displaying Calendar in an Android App Hi i want to show a

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.