在mapview上绘制路线的最简单方法

时间:2012-12-17 09:17:50

标签: android android-mapview

我想显示用户当前位置与他在searchview中输入的位置之间的路线。这是我的onQueryTextSubmit方法的代码

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

最简单的方法是什么? (例子会帮助我理解更多)

谢谢

2 个答案:

答案 0 :(得分:4)

获得目标GeoPoint后,您可以在此地址中执行google http请求:

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

是原点和目的地(纬度,经度)点。

例如,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@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yEzEiv@X_Cz@aEpBaGtB{DvRmW`BaC`AoBbAsCl@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}]}

名称为points的字符串是您必须在地图上绘制的折线。但在绘图之前你必须解码它。看看here是否有责任。

获得GeoPoint列表后,您可以在地图上绘制它。为此,如果您使用的是谷歌api v1,则需要overlay。我建议你看看api v2。通过GoogleMap类和PolylineOptions中的addPolyline方法向地图对象添加折线更容易。

希望有所帮助。

答案 1 :(得分:0)

我们可以通过两种方式在地图上绘制路线。

1.在android中使用Map OverLay概念 2.使用此网址 http://maps.google.com/maps?&saddr=from纬度值,从经度值& daddr =到纬度值,到经度值