Simplest way to draw a route on mapview

2019-08-13 03:18发布

问题:

I want to show a route between the user's current location and the location he entered in the searchview. Here is my code for the onQueryTextSubmit method

@Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        try
        {
            if(direction.isChecked() == false)
            {
                gc = new Geocoder(this);

                addresses = gc.getFromLocationName(query,5);
                String add = "";
                if (addresses.size() > 0) 
                {
                    p = new GeoPoint(
                            (int) (addresses.get(0).getLatitude() * 1E6), 
                            (int) (addresses.get(0).getLongitude() * 1E6));
                    mc.animateTo(p);
                    mc.setZoom(9);
                    mapView.invalidate();

                    searchView.clearFocus();
                }   
                else
                {
                     Toast.makeText(this, " Location not found.",
                                Toast.LENGTH_LONG).show();
                }
                return true;
            }
            else
            {
            // draw a route between the locations                    
                 return true;
            }
        }
        catch(IOException ex)
        {

        }

        return false;
    }

what is the simplest way to do it? (examples will help me understand more)

Thank You

回答1:

Once you have your destination GeoPoint you could do an http request to google in this address:

String url = "http://maps.google.com/maps?f=d&hl=es&saddr="+ origin +"&daddr=" + destination + "&ie=UTF8&om=0&output=dragdir";

being origin and destination (latitude, longitude) points.

For example, this request gives you this json:

{tooltipHtml:" (86,4 km / 55 minutos)",polylines:[{id:"route0",points:"wa{uFt|kUoAkE??jDyE??uFuLqFaH??mAl@gAZ??q@[Yg@KwCBkATq@X[bCu@??tMsBrIuBrDoA~J}EbC{@pCy@dIeA`JBrDThd@nFpDRnTkAdBGVFhRyC`McA|KkAzCK|CP??lA~@NZHn@I~@Q`@c@\\[Hu@KYWUe@Gc@D{BnG}a@`Kcb@|A}JvAkOXmHDgFKcMMmDkBgW??SaCEoFBaCb@mH`AwGdE_Qh@sCf@}Db@yGFcFIqE]iGcAkLUyGB{K^aHf@_EfC_PtAqOlB}X|AiNrCyTnE{WjDqOtCqK~DoMhHoSvX}u@~KeYpI}VrSkk@zBiJ`@_Ct@oG`@mGPoGCcLMoDq@wHiCaSe@qE_BgTMuEEoIDsJxA_`@DcHGoCw@iJkAoHsCwN_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l@eCf@iDn@}G`@mC`AwD~AuDhJ{OdCuGfA{Ej@eEZwDzEgu@x@sJ^qBj@kBdD}HbAkDhEaX^gDJgC?iCKiCw@iIK_EL}Db@qDbAqD|@uBjOuVxIgQ~DsK|AuFpAaHt@_F`AqK^gL@yG_@u\\HeHNwEv@oKx@eHbDgPzDuNlAuD~F{O~BoF`AiBnBoCzFwF~CoDlBcEfAaD|Kc_@r@yBtAgD`AaBbHsJ|CsCrP_LpBwB`BqChAgDr@yDR}BhBs\\VuBx@kDlAyCdBkCdDqClQcHlC_B|CuDlJcQbA{AlAqA~_Ags@lSiSrBaCfOiWtVs]z`@_s@zMiVvAuBzPyTfRg[rBuD`B_EtCkIvKs]nCaHlEuIfHaMbMoRv[kd@lEqFfo@qp@|C{Cf@[bQsHrBoA~AsAbAqA|GsMtBiC|B_BlHwD`Au@vA{AnAmB|@wB|@eDbEmThAsEh@_BzAcDnKmP~z@}kA|AcBbDcCvFyBvSgGnBa@~TiGnNmC|DkB??xCoApDU|@UtAeAZm@\\gARkBQeK@i@^cC??pBkHvFy]Pm@ZCHQVuAAc@hAyC~@m@dBYzPq@??Jd@NJR?VSBi@pBq@l@e@d@e@jAoB\\_A\\_BnBePTCN[A[SWtIsr@Aq@VCLSAg@OQpDwX^kE??rAJrB]p@[tAuA",levels:"BBBBB?BB?BB???@??BB??????@???@???????BB??@???@????@???@????BB???@????@??????@??????@??@????@??A???@????@????@?????@????A?????@?????@???@???@?????@???A?@???@????@???@???@???A?????@????@??????????@??????@????@?????@???@???@?????BB??@???@??????@????@??@???@??A???@?????@????@?@??????????@???A???@?????@?????@???@?????@???????@??@???@???@????@???@????@???@???@??@????A????@????@????@?????@???@???@?????@??????@???@???@?????@??@???@????@???@?????@??????A???@???@??@??????@????@??@?@@???@???@???@??????@?@??@????BB????@????BB???????@??BB??????@???@????????????BB???B",numLevels:4,zoomFactor:16}]}

The String with name points is the polyline you have to draw on your map. But before drawing you have to decode it. Take a look here for that duty.

Once you have your GeoPoint list, you can draw it on your map. For that you need an overlay if you're using google api v1. I'd recommend you to have a look into api v2. It's easier to add polylines to map objects via the addPolyline method in GoogleMap class and PolylineOptions.

Hope that helps.



回答2:

by two ways we can draw route on map.

1.use Map OverLay concept in android 2.use this url http://maps.google.com/maps?&saddr=from latitude value,from longitude value &daddr= to latitude value,to longitude value