2012-12-17 87 views
1

我想要顯示用戶當前位置與他在搜索視圖中輸入的位置之間的路線。這裏是我的代碼爲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; 
    } 

什麼是最簡單的方法呢? (例子會幫助我瞭解更多)

謝謝

回答

4

一旦你有你的目的地的GeoPoint你可以做一個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|[email protected][email protected][[email protected]@X[[email protected]??tMsBrIuBrDoA~J}EbC{@[email protected]`[email protected]`McA|KkAzCK|[email protected]@[email protected]`@[email protected]\\[[email protected]@[email protected]{BnG}[email protected]`[email protected]|A}[email protected]`[email protected]@}[email protected]]iGcAkLUyGB{K^[email protected]_EfC_PtAqOlB}X|AiNrCyTnE{WjDqOtCqK~DoMhHoSvX}[email protected]~KeYpI}[email protected]`@[email protected]`@[email protected]@qE_BgTMuEEoIDsJxA_`@[email protected][email protected]]wGOcLLeJZiG\\[email protected]{[email protected]{F~CuGbBuCdE_GlE}[email protected][email protected]@iFZeJBiYJ{[email protected]{HpAaMdDwRzB{IrCsIrCoGbHiMzGmJnEaFjCeCdCwBfE}CdC_BzHiErNyG~NsFdN}DnQuDzK}[email protected][email protected]|GaGtEiF~GwIfGsGdD{CtEcDpQwKvCwBdFeE|HmIxEoGtCwElLiVfDeGxDkFvNsOvDaFbDqFnI{PtBwDfDiF`FkG~UeVfC{ClD{EpMyRzDyEdE_EvHwFpGiDfDsA|[email protected]@[email protected]_BnGmCpHiEbVgPzBmBhBoB|B_D|AqCdAaCtGaRvAuC~EeI??jSk[zBcEnAuCnD}[email protected]`[email protected]@[email protected][email protected]@qBcB}[email protected]?kEJuCR}[email protected]@qCxAqDtCmEtAwAfCuB|[email protected]@[email protected][email protected]{DfDuC`[email protected]`[email protected]@vD}ArA{@xBmBlBcC~A{[email protected]|@oD^}[email protected]@{BzAwDzBsDpBaCnBiBlB_CnFoIfD_ExAoAfEgC`[email protected]{FhByDtBsFZcBPiBTqIRiB\\[email protected]@uA`AeApBqAtB]xBExS`@[email protected]@[email protected]@[email protected]@[email protected]}[email protected]{QlE}FnFeF`CeBrDsB`EaBlDaApE{@[email protected]{[email protected]{@hHwIdOqRnA{[email protected]@fBmFzAcGtA{I`@[email protected][email protected]{DvRmW`BaC`[email protected]@[email protected]}G`@mC`AwD~AuDhJ{OdCuGfA{[email protected]@[email protected]^[email protected]}HbAkDhEaX^[email protected]_EL}[email protected]|@uBjOuVxIgQ~DsK|[email protected]_F`AqK^[email protected][email protected]\\[email protected]@eHbDgPzDuNlAuD~F{O~BoF`AiBnBoCzFwF~CoDlBcEfAaD|[email protected]@yBtAgD`AaBbHsJ|CsCrP_LpBwB`[email protected]}BhBs\\[email protected]_B|CuDlJcQbA{[email protected]]z`@[email protected][rBuD`B_EtCkIvKs]nCaHlEuIfHaMbMoRv[[email protected]@[email protected]|C{[email protected][bQsHrBoA~AsAbAqA|GsMtBiC|B_BlHwD`[email protected]{AnAmB|@wB|@[email protected][email protected]}kA|[email protected]~TiGnNmC|DkB??xCoApDU|@[email protected]\\[email protected]@^cC??pBkHvFy][email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@jAoB\\_A\\_BnBePTCN[A[[email protected]@[email protected]^kE??rAJrB][email protected][tAuA",levels:"[email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected]@[email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected]????????????BB???B",numLevels:4,zoomFactor:16}]} 

字符串以名稱points是你要畫在地圖上的折線。但在繪製之前,您必須對其進行解碼。看看here這個職責。

一旦你有你的GeoPoint列表,你可以在你的地圖上繪製它。爲此,如果您使用google api v1,則需要overlay。我建議你看看api v2。通過GoogleMap類和PolylineOptions中的addPolyline方法添加多段線以映射對象更爲容易。

希望有所幫助。

+0

感謝名單了很多....跟隨這些鏈接,終於得到它的工作 –

+0

很高興我可以幫助:) –

0

通過兩種方式我們可以在地圖上繪製路線。

1.使用android系統中 地圖疊加的概念2.使用這個網址 http://maps.google.com/maps?&saddr=from緯度值,經度從價值& DADDR =緯度值,經度值

相關問題