web-dev-qa-db-fra.com

Comment analyser des points et tracer un itinéraire sur une carte Google Map sous Android?

Comment obtenir des points et dessiner l'itinéraire sur Google maps dans Android OS?

52
Matt Rutkowsky

Un exemple d'URL que vous allez analyser est ...

http://maps.googleapis.com/maps/api/directions/xml?origin=52.31,16.71&destination=51.27,6.75&sensor=false

changez l'origine [lat, long] et la destination [lat, long] selon vos besoins

-

récupération de points

import Java.io.StringReader;
import Java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import net.gynsoft.citydusseldorf.lib.HttpClient;
import Android.os.Handler;
import com.google.Android.maps.GeoPoint;


public class MapRoute 
{
private GeoPoint gpSrc = null;
private GeoPoint gpDest = null;
private ArrayList<GeoPoint> alRoute = new ArrayList<GeoPoint>();
private Handler haRoute = new Handler();

public interface RouteListener
{
    public void onDetermined(ArrayList<GeoPoint> alPoint);
    public void onError();
}

private RouteListener oRoute = null;


public MapRoute(GeoPoint gpSrc,GeoPoint gpDest)
{
    this.gpSrc = gpSrc;
    this.gpDest = gpDest;
}

public void getPoints(RouteListener oRoute)
{
    this.oRoute = oRoute;
    new Thread(ruFetch).start();
}

private Runnable ruFetchOk = new Runnable()
{
    public void run()
    {
        oRoute.onDetermined(alRoute);
    }
};

private Runnable ruFetchError = new Runnable()
{
    public void run()
    {
        oRoute.onDetermined(alRoute);
    }
};

private Runnable ruFetch = new Runnable()
{
    public void run()
    {
        String szUrl = "http://maps.googleapis.com/maps/api/directions/xml";
        szUrl += "?origin=" + (gpSrc.getLatitudeE6()/1e6) + "," + (gpSrc.getLongitudeE6()/1e6);
        szUrl += "&destination=" + (gpDest.getLatitudeE6()/1e6) + "," + (gpDest.getLongitudeE6()/1e6);
        szUrl += "&sensor=true";

        HttpClient oHttp = HttpClient.getInstance();
        String szXml = oHttp.doGet(szUrl,"");

        try
        {
            XmlPullParserFactory xppfFactory = XmlPullParserFactory.newInstance();
            xppfFactory.setNamespaceAware(true);
            XmlPullParser xppParses = xppfFactory.newPullParser();

            xppParses.setInput(new StringReader(szXml));
            int iEventType = xppParses.getEventType();
            String szTag = "";
            String szText = "";
            boolean bStep = false;
            int iLat = 0;
            int iLong = 0;

            while(iEventType != XmlPullParser.END_DOCUMENT) 
            {     
                 iEventType = xppParses.next();

                 if(iEventType == XmlPullParser.START_TAG) 
                 {
                     szTag = xppParses.getName();

                     if(szTag.equals("step"))
                         bStep = true;
                 }
                 else if(iEventType == XmlPullParser.TEXT)      
                 {  
                     if(szTag.equals("points"))
                         szText = "";
                     else
                         szText = xppParses.getText().trim();
                 }          
                 else if(iEventType == XmlPullParser.END_TAG) 
                 {
                     if(xppParses.getName().equals("step"))
                     {
                         bStep = false;
                     }
                    else if(bStep && xppParses.getName().equals("start_location") || xppParses.getName().equals("end_location"))
                     {
                         GeoPoint gpPoint = new GeoPoint(iLat,iLong);
                         alRoute.add(gpPoint);
                     }
                     else if(bStep && xppParses.getName().equals("lat"))
                     {
                         iLat = (int)(Double.parseDouble(szText) * 1e6);
                     }
                     else if(bStep && xppParses.getName().equals("lng"))
                     {
                         iLong = (int)(Double.parseDouble(szText) * 1e6);
                     }
                 }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            haRoute.post(ruFetchError);
        }


        if(alRoute.size() == 0)
            haRoute.post(ruFetchError);
        else
            haRoute.post(ruFetchOk);
    }
};
}

-

superposition de carte

public class MapRouteOverlay extends Overlay 
{
private GeoPoint gp1;
private GeoPoint gp2;

private int mode=0;
private int defaultColor;

public MapRouteOverlay(GeoPoint gp1,GeoPoint gp2,int mode) // GeoPoint is a int. (6E)
{
this.gp1 = gp1;
this.gp2 = gp2;
this.mode = mode;
defaultColor = 999; // no defaultColor

}

public MapRouteOverlay(GeoPoint gp1,GeoPoint gp2,int mode, int defaultColor)
{
this.gp1 = gp1;
this.gp2 = gp2;
this.mode = mode;
this.defaultColor = defaultColor;
}

public int getMode()
{
return mode;
}

public boolean draw (Canvas canvas, MapView mapView, boolean shadow, long when)
{
    Projection projection = mapView.getProjection();
    if (shadow == false)
    {

        Paint paint = new Paint();
        Paint.setAntiAlias(true);
        Paint.setDither(true);
        Point point = new Point();
        projection.toPixels(gp1, point);

        if(mode==2)
        {
            if(defaultColor==999)
            {
                Paint.setColor(Color.RED);
            }
            else
            {
                Paint.setColor(defaultColor);
                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);
            }
        }
    }
    return super.draw(canvas, mapView, shadow, when);
}
}

-

comment l'utiliser ... quelque part dans mon activité

dEmpty = new BitmapDrawable(Bitmap.createBitmap(1,1,Bitmap.Config.ARGB_8888));
dPin = getResources().getDrawable(R.drawable.pin_map);

private void doDrawPath(GeoPoint gpSrc,GeoPoint gpDest) 
{
    MapRoute oRoute = new MapRoute(gpSrc,gpDest);
    oRoute.getPoints(new RouteListener()
    {
        @Override
        public void onDetermined(ArrayList<GeoPoint> alPoint) 
        {
            GeoPoint oPointA = null;
            GeoPoint oPointB = null;

            mvMap.getOverlays().clear();

            for(int i=1; i<alPoint.size()-1; i++)
            {
                oPointA = alPoint.get(i-1);
                oPointB =  alPoint.get(i);

                mvMap.getOverlays().add(new MapRouteOverlay(oPointA,oPointB,2,Color.RED));
            }


            mvMap.getOverlays().add(new MapRoutePinOverlay(alPoint.get(0),dPin));
            mvMap.getOverlays().add(new MapRoutePinOverlay(alPoint.get(alPoint.size()-1),dPin));

            mvMap.invalidate();
        }

        @Override
        public void onError() 
        {
        }           
    });
}
1
Matt Rutkowsky