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 8554963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:00:29+00:00 2026-06-11T15:00:29+00:00

I have code written by Hesham Saeed. I want to put the direction function

  • 0

I have code written by Hesham Saeed. I want to put the direction function in AsyncTask as it is using Network. How can i do that as i m new to Android.
I need to move direction in AsyncTask..

Main Activity Code.

package com.example.googlemaptst;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

public class MapsActivity extends MapActivity
{    

/** Called when the activity is first created. */
Button b;
private ProgressDialog pDialog;
private  Route directions(final GeoPoint start, final GeoPoint dest)  {
   Parser parser;
    String jsonURL = "http://maps.google.com/maps/api/directions/json?";
    final StringBuffer sBuf = new StringBuffer(jsonURL);
    sBuf.append("origin=");
    sBuf.append(start.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(start.getLongitudeE6()/1E6);
    sBuf.append("&destination=");
    sBuf.append(dest.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(dest.getLongitudeE6()/1E6);
    sBuf.append("&sensor=true&mode=driving");
    Log.v("I came in URL", sBuf.toString());
    parser = new GoogleParser(sBuf.toString());
    Route r =  parser.parse();
    return r;
}

@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_map);
     MapView mapView = (MapView) findViewById(R.id.mapView); //or you can declare it directly with the API key
     Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));
     RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
     mapView.getOverlays().add(routeOverlay);

}

@Override
protected boolean isRouteDisplayed() {

    return false;
}
}
  • 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-11T15:00:30+00:00Added an answer on June 11, 2026 at 3:00 pm

    Ok, here’s what you have to do:

        MapView mapView = null;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_map);
         mapView = (MapView) findViewById(R.id.mapView); //or you can declare it directly with the API key
         DirectionsTask getDirectionsTask = new DirectionsTask();
         getDirectionsTask.execute(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));
    
    }
    
    private class DirectionsTask extends AsyncTask<GeoPoint, Void, Route> {
    
        protected Route doInBackground(GeoPoint... geoPointParams) {
            GeoPoint start = geoPointParams[0];
            GeoPoint dest = geoPoint[1];
    
               Parser parser;
                String jsonURL = "http://maps.google.com/maps/api/directions/json?";
                final StringBuffer sBuf = new StringBuffer(jsonURL);
                sBuf.append("origin=");
                sBuf.append(start.getLatitudeE6()/1E6);
                sBuf.append(',');
                sBuf.append(start.getLongitudeE6()/1E6);
                sBuf.append("&destination=");
                sBuf.append(dest.getLatitudeE6()/1E6);
                sBuf.append(',');
                sBuf.append(dest.getLongitudeE6()/1E6);
                sBuf.append("&sensor=true&mode=driving");
                Log.v("I came in URL", sBuf.toString());
                parser = new GoogleParser(sBuf.toString());
                Route r =  parser.parse();
                return r;
        }
    
    
    }
    
    protected void onPostExecute(Route route) {
        RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
         mapView.getOverlays().add(routeOverlay);
    }
    

    A couple of comments. Put mapView outside of OnCreate so onPostExecute has access to it. Note that onPostExecute gets called in the UI thread so that’s ok.

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

Sidebar

Related Questions

I have written code to perform a function that could take a while to
Ok, so I have this code written in C++ that call the function SetThreadDesktop
I am using InfoPath 2007 I have some code written in the form that
I have code written using GNU Pth ( http://www.gnu.org/software/pth/ ) and I want to
I have code written in c, which contains some complex algorithms and I want
I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
I'm using some not optimal code written by me... :-| I have following code:
I have written code that opens 16 figures at once. Currently, they all open
I have a code written in VB.Net that will run on multiple systems. One
I have a code written using OpenCV in C++, and this code uses 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.