How can I parse out points and draw a route on a G

2019-01-20 23:07发布

问题:

How do I get points & draw the route on Google maps in Android OS?

回答1:

An example URL you're going to parse is...

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

change origin [lat,long] and destination[lat, long] for your purpose

--

points fetching

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);
    }
};
}

--

map overlay

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);
}
}

--

how to use it... somewhere in my activity

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() 
        {
        }           
    });
}