2015-12-15 26 views
3

我想提請使用的谷歌Android map.I兩個位置之間的路徑已經厭倦了這段代碼如何得出兩個位置的android

PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true); 

     if (cursor.moveToFirst()) { 
      do { 

       // latitude and longitude 
       double latitude = 0.00; 
       double longitude = 0.00; 


       Log.i("lat", cursor.getString(10)); 
       Log.i("lat", cursor.getString(11)); 


       try { 
        latitude = Double.parseDouble(cursor.getString(10)); 
       } catch (Exception e) { 

       } 

       try { 
        longitude = Double.parseDouble(cursor.getString(11)); 
       } catch (Exception e) { 

       } 
       if (latitude == 0.00 || longitude == 0.00) { 

       } else { 
        i++; 





         LatLng point = new LatLng(latitude,longitude); 
         options.add(point); 



        // create marker 
        MarkerOptions marker = new MarkerOptions().position(
          new LatLng(latitude, longitude)).title(cursor.getString(1)); 

       } 

      } 
      while (cursor.moveToNext()); 
     } 
     cursor.close(); 


     googleMap.addPolyline(options); 

,但我得到直line.like這

之間實際道路路

my map view

我想在實際道路上獲得路徑。

謝謝.. !!

回答

1

如果你想畫2點和沿路之間的折線,你可以與圖書館嘗試Google-Directions-Android

您可以compile 'com.github.jd-alexander:library:1.0.7'

的gradle上添加庫可以使用所有的點(經緯度),並利用它們進入航點方法。

Routing routing = new Routing.Builder() 
       .travelMode(/* Travel Mode */) 
       .withListener(/* Listener that delivers routing results.*/) 
       .waypoints(/*waypoints*/) 
       .build(); 
    routing.execute(); 
+0

我可以使用相同的代碼嗎? –

+0

是的。你正在做的是獲得你的所有觀點(Latlng),並把它們放到路點方法中。 – xiaomi

+0

雖然gradle building顯示錯誤 –

0

請檢查這個答案,讓我知道,如果這是你所需要的:

https://stackoverflow.com/a/17007360/2027644

+0

給我一個錯誤 –

+0

請詳細說明錯誤 –

+0

錯誤是谷歌服務器中未發現java.io.FileNotFoundException:http://maps.googleapis.com/maps/api/directions/json –