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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:39:39+00:00 2026-06-01T21:39:39+00:00

I am able to draw path between two sets of geo-coordinates from reference of

  • 0

I am able to draw path between two sets of geo-coordinates from reference of
j2memaprouteprovider.
I am able to get current location latitude and longitude.
How do i implement these coordinates in the following class to get path drawn from current position to target position.

MapRouteActivity.java

public class MapRouteActivity extends MapActivity {

LinearLayout linearLayout;
MapView mapView;
private Road mRoad;
static Context context = null;
String GPSPROVIDER = LocationManager.GPS_PROVIDER;
private static final long MIN_GEOGRAPHIC_POOLING_TIME_PERIOD = 10000;
private static final float MIN_GEOGRAPHIC_POOLING_DISTANCE = (float) 5.0;
public LocationManager gpsLocationManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    context = this;

    /* Get a listener for GPS */
    LocationListener gpsLocationListener = null;

    gpsLocationListener = new GpsLocationListener(this);

    /* Start Location Service for GPS */
    gpsLocationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);

    /* Register GPS listener with Location Manager */
    gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            MIN_GEOGRAPHIC_POOLING_TIME_PERIOD,
            MIN_GEOGRAPHIC_POOLING_DISTANCE, gpsLocationListener);
    if (gpsLocationManager == null) {
        gpsLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER,
                MIN_GEOGRAPHIC_POOLING_TIME_PERIOD,
                MIN_GEOGRAPHIC_POOLING_DISTANCE, gpsLocationListener);
    }

    boolean isAvailableGps = gpsLocationManager
            .isProviderEnabled(GPSPROVIDER);

    if (isAvailableGps) {

        Location loc = gpsLocationManager.getLastKnownLocation("gps");

        if (loc != null) {

            double lattitude = loc.getLatitude();
            double longitude = loc.getLongitude();

            Toast.makeText(
                    MapRouteActivity.this,
                    "Longitude iss  " + lattitude + "   Latitude iss   "
                            + longitude, Toast.LENGTH_LONG).show();

        }
    }
    new Thread() {
        @Override
        public void run() {
            double fromLat = 49.85, fromLon = 24.016667, toLat = 50.45, toLon = 30.523333;
            String url = RoadProvider
                    .getUrl(fromLat, fromLon, toLat, toLon);
            InputStream is = getConnection(url);
            mRoad = RoadProvider.getRoute(is);
            mHandler.sendEmptyMessage(0);
        }
    }.start();

}

Handler mHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        TextView textView = (TextView) findViewById(R.id.description);
        textView.setText(mRoad.mName + " " + mRoad.mDescription);
        MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();
    };
};

private InputStream getConnection(String url) {
    InputStream is = null;
    try {
        URLConnection conn = new URL(url).openConnection();
        is = conn.getInputStream();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return is;
}

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

public static class GpsLocationListener implements LocationListener {
    private ImageView mCurrentPointer;

    public GpsLocationListener(Context context) {

    }

    public void onLocationChanged(Location loc) {

        if (loc != null) {

            double latitude = loc.getLatitude();
            double longitude = loc.getLongitude();
            GeoPoint point = new GeoPoint((int) (latitude * 1E6),
                    (int) (longitude * 1E6));
            Toast.makeText(
                    context,
                    "Longitude is  " + longitude + "   Latitude is   "
                            + latitude, Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

}
}

class MapOverlay extends com.google.android.maps.Overlay {
Road mRoad;
ArrayList<GeoPoint> mPoints;

public MapOverlay(Road road, MapView mv) {
    mRoad = road;
    if (road.mRoute.length > 0) {
        mPoints = new ArrayList<GeoPoint>();
        for (int i = 0; i < road.mRoute.length; i++) {
            mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000),
                    (int) (road.mRoute[i][0] * 1000000)));
        }
        int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
                mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
                .getLatitudeE6()) / 2);
        int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
                mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
                .getLongitudeE6()) / 2);
        GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);

        MapController mapController = mv.getController();
        mapController.animateTo(moveTo);
        mapController.setZoom(7);
    }
    }

@Override
public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
    super.draw(canvas, mv, shadow);
    drawPath(mv, canvas);
    return true;
}

public void drawPath(MapView mv, Canvas canvas) {
    int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
    Paint paint = new Paint();
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(3);
    for (int i = 0; i < mPoints.size(); i++) {
        Point point = new Point();
        mv.getProjection().toPixels(mPoints.get(i), point);
        x2 = point.x;
        y2 = point.y;
        if (i > 0) {
            canvas.drawLine(x1, y1, x2, y2, paint);
        }
        x1 = x2;
        y1 = y2;
    }
}
}

I want to assign current position lattiude and longitude value to fromLat and fronLon variable in above code here:

new Thread() {
        @Override
        public void run() {
            double fromLat = 49.85, fromLon = 24.016667, toLat = 50.45, toLon = 30.523333;
            String url = RoadProvider
                    .getUrl(fromLat, fromLon, toLat, toLon);
            InputStream is = getConnection(url);
            mRoad = RoadProvider.getRoute(is);
            mHandler.sendEmptyMessage(0);
        }
    }.start();

Help !!

  • 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-01T21:39:41+00:00Added an answer on June 1, 2026 at 9:39 pm

    Its very Simple—

    Make your Activity like this:–

     public class GoogleMapLocationActivity extends MapActivity {
    
     private LocationManager myLocationManager;
     private LocationListener myLocationListener;
     private TextView myLongitude, myLatitude;
     private MapView myMapView;
     private MapController myMapController;
     LinearLayout zoomLayout;
     GeoPoint myLastPosition;
     AddItemizedOverlay mapOvlay;
    
     private void CenterLocatio(GeoPoint centerGeoPoint)
     {
      myMapController.animateTo(centerGeoPoint);
    
      List<Overlay> mapOverlays = myMapView.getOverlays();
      Drawable drawable = this.getResources().getDrawable(R.drawable.map_point);
    
      if(myLastPosition != null){
      mapOvlay = new AddItemizedOverlay(myLastPosition ,centerGeoPoint,drawable );
      OverlayItem overlayitem = new OverlayItem(centerGeoPoint,"","" );
      mapOvlay.addOverlay(overlayitem);
      mapOverlays.add(mapOvlay);
      }
          myLastPosition = centerGeoPoint;
    
    };
    
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      myMapView = (MapView)findViewById(R.id.mapview);
             myLastPosition = null;
         myMapController = myMapView.getController();
         myMapController.setZoom(18); //Fixed Zoom Level
    
      myLocationManager = (LocationManager)getSystemService(
        Context.LOCATION_SERVICE);
    
      myLocationListener = new MyLocationListener();
    
      myLocationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER,
        0,
        0,
        myLocationListener);
    
     private class MyLocationListener implements LocationListener{
    
          public void onLocationChanged(Location argLocation) {
            GeoPoint myGeoPoint = new GeoPoint(
            (int)(argLocation.getLatitude()*1000000),
             (int)(argLocation.getLongitude()*1000000));
    
         GeoPoint newGeoPoint = new  GeoPoint(myGeoPoint.getLatitudeE6() ,myGeoPoint.getLongitudeE6());
       CenterLocatio(newGeoPoint);
      }
    
      public void onProviderDisabled(String provider) {
          Toast.makeText(getBaseContext(),"GPS Disabled" ,Toast.LENGTH_SHORT).show();
      }
    
      public void onProviderEnabled(String provider) {
          Toast.makeText(getBaseContext(),"GPS Enabled" ,Toast.LENGTH_SHORT).show();
      }
    
      public void onStatusChanged(String provider,
        int status, Bundle extras) {
          Toast.makeText(getBaseContext(),"GPS Unavailable" ,Toast.LENGTH_SHORT).show();
      }
     }
    
     @Override
     protected boolean isRouteDisplayed() {
      return false;
     };
    
    }
    

    AddItemizedOverlay.class—–

    public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
    
           private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
    
           private Context context;
           private GeoPoint mGpt1;
           private GeoPoint mGpt2;
    
           public AddItemizedOverlay(GeoPoint gp1,GeoPoint gp2, Drawable defaultMarker) {
    
                super(boundCenterBottom(defaultMarker));
                 mGpt1 = gp1;
                 mGpt2 = gp2;
            }
    
    
           public AddItemizedOverlay(Drawable defaultMarker) {
                super(boundCenterBottom(defaultMarker));
           }
    
           public AddItemizedOverlay(Drawable defaultMarker, Context context) {
                this(defaultMarker);
                this.context = context;
           }
    
           @Override
           protected OverlayItem createItem(int i) {
              return mapOverlays.get(i);
           }
    
           @Override
           public int size() {
              return mapOverlays.size();
           }
    
           @Override
           protected boolean onTap(int index) {
              Log.e("Tap", "Tap Performed");
              return true;
           }
    
           public void addOverlay(OverlayItem overlay) {
              mapOverlays.add(overlay);
               this.populate();
           }
    
           public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                    long when) {
                 super.draw(canvas, mapView, shadow);
                 Paint paint;
                 paint = new Paint();
                 paint.setColor(Color.RED);
                 paint.setAntiAlias(true);
                 paint.setStyle(Style.STROKE);
                 paint.setStrokeWidth(2);
                 Point pt1 = new Point();
                 Point pt2 = new Point();
                 Projection projection = mapView.getProjection();
                 projection.toPixels(mGpt1, pt1);
                 projection.toPixels(mGpt2, pt2);
                 canvas.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, paint);
                 return true;
              }
        }
    

    Hope it will help you…

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

Sidebar

Related Questions

I am able to get nearby places but, how do i draw the places
I want to draw a route between two locations on the map. Something like
Anyone has sample code for drawing a rubber band style path between two shapes
I want to be able to draw an array of balls, I can push
I'm learning OpenGL and SDL and so far I've been able to properly draw
I want to be able to email a report daily from a glpi database
I am able to read emails in from Microsoft Exchange using an IMAP Client
I want to make an application in which I can draw a path on
I want to be able to draw on to the top of a TextBlock,
I'm using php and gd library to draw a triangle. I'm able to draw

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.