Skip to content Skip to sidebar Skip to footer

How To Assign Current Geo-coordinates To Draw Path From Current Location To Target Location?

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 thes

Solution 1:

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...


Solution 2:

Use following, It will display direction on native map application,

 final Intent intent = new Intent(Intent.ACTION_VIEW,
    Uri.parse(
            "http://maps.google.com/maps?" +
            "saddr="+YOUR_START_LONGITUDE+","+YOUR_START_LATITUDE+"&daddr="YOUR_END_LONGITUDE+","+YOUR_END_LATITUDE));
         intent.setClassName(
          "com.google.android.apps.maps",
          "com.google.android.maps.MapsActivity");
   startActivity(intent);

Post a Comment for "How To Assign Current Geo-coordinates To Draw Path From Current Location To Target Location?"