0

I've created my own custom map to show my current location and multiple points on map. it works fine,but now i would like to show a route from my current location to every point on the map.Can you give me some suggestions how i can do that? thx.

this is my customMap activity:

public class CustomMapActivity extends MapActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

         String   provider = ""; 
         Criteria crit = new Criteria();
         Location loc = new Location("");
         LocationManager mlocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

         MapView mapView = (MapView) findViewById(R.id.mapview);
         mapView.setBuiltInZoomControls(true);

         List<Overlay> mapOverlays = mapView.getOverlays();
         Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
         ItemizedOverlayedClass itemizedoverlay = new ItemizedOverlayedClass(drawable, this);

            try
            { 
             MyLocationOverlay mylocationOverlay = new MyLocationOverlay(this, mapView);
             mylocationOverlay.enableMyLocation();
             mapView.getOverlays().add(mylocationOverlay);          
             provider = mlocManager.getBestProvider(crit, false);
             loc = mlocManager.getLastKnownLocation(provider);
            }
            catch(Exception ex)
            {
            //Toast.makeText(this, "Unable to retrive provider", Toast.LENGTH_LONG).show();
            }


        for (ServiceActivity activity : DataSources.ActivitiesList)
        { 
            try
            {
                Location siteLocation =  new Location("");
                siteLocation.setLatitude(activity.SiteLatitude);
                siteLocation.setLongitude(activity.SiteLongitude);

                GeoPoint point = new GeoPoint((int)(activity.SiteLatitude * 1e6),(int)(activity.SiteLongitude * 1e6));
                OverlayItem overlayitem = new OverlayItem(point, activity.SiteName+ " " + activity.SiteAddress, "Distance to this location: " + String.valueOf(loc.distanceTo(siteLocation)/1000) + " km");

                itemizedoverlay.addOverlay(overlayitem);
                mapOverlays.add(itemizedoverlay);
            }
            catch(Exception ex)
            {
                @SuppressWarnings("unused")
                AlertDialogClass alert =  new AlertDialogClass(this,ex.getMessage());
            }
        }   




    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        //mylocationOverlay.enableMyLocation();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_map, menu);
        return true;
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }


}

and this is the overlay class:

package com.example.srwebservice;


import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class ItemizedOverlayedClass extends ItemizedOverlay {

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

    public ItemizedOverlayedClass(Drawable defaultMarker) 
    {
        super(boundCenterBottom(defaultMarker));
        // TODO Auto-generated constructor stub
    }

    public ItemizedOverlayedClass(Drawable defaultMarker, Context context) 
    {
      super(boundCenterBottom(defaultMarker));
      mContext = context;
    }

    @Override
    protected OverlayItem createItem(int i) 
    {
        // TODO Auto-generated method stub
         return mOverlays.get(i);
    }

    @Override
    public int size()
    {
      return mOverlays.size();
    }

    public void addOverlay(OverlayItem overlay)
    {
        mOverlays.add(overlay);
        populate();
    }
    @Override
    protected boolean onTap(int index) {
      OverlayItem item = mOverlays.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.show();
      return true;
    }
}
Roman Marius
  • 456
  • 3
  • 9
  • 21

2 Answers2

2

This link have tutorial to draw route path on map in our app. this may help you and use below to link with map app... this will take to you in google map application

String uri = "http://maps.google.com/maps?f=d&hl=en&saddr="+source_latitude+","+source_longitude+"&daddr="+destination_latitude+","+destination_longitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);

OR

refter this link how to display the driving routes

Community
  • 1
  • 1
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
2

i found a simpler solution.I can use google maps intent directly, and i don't need to create a custom map activity anymore:

if(DataSources.ActivitiesList.length >0)
{
  String address = "http://maps.google.com/maps?daddr=" +    DataSources.ActivitiesList[0].SiteLatitude.toString() + "," + DataSources.ActivitiesList[0].SiteLongitude.toString();
for (int i= 1 ;i <  DataSources.ActivitiesList.length ; i++) 
{
    if(DataSources.ActivitiesList[i].SiteLatitude != null)
        address += "+to:" + DataSources.ActivitiesList[i].SiteLatitude + "," + DataSources.ActivitiesList[i].SiteLongitude;
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(address));
    startActivity(intent);
    break;      
}
Roman Marius
  • 456
  • 3
  • 9
  • 21