2015-04-01 53 views
-4

我試圖開發一個帶有地圖的android應用程序。我是Android應用程序開發的初學者,我不知道如何在我的當前位置和目的地之間生成路徑。我可以通過下面顯示的代碼查看當前位置。在我的地圖中顯示路徑android app

package com.example.myapp; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.model.LatLng; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

public class Grandinmaps extends Activity implements OnMapReadyCallback{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_grandinmaps); 
     MapFragment mapFragment = (MapFragment) getFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    @Override 
    public void onMapReady(GoogleMap map) { 
     // TODO Auto-generated method stub 
     map.setMyLocationEnabled(true); 
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(
       new LatLng(12.971907, 80.220462), 18)); 

    } 
} 

而我不知道代碼來生成到目的地的路徑。 任何幫助...... !!!請:-)

回答

0

繪製路線,你可以使用此代碼:

徵集印刷位置的路線:

GmapV2Direction route; 
    Document document; 

LatLng toPosition = new LatLng(routeData.get(i).latitude, routeData.get(i).longitude); 
         new GetRouteTask() { 
          @Override 
          protected void onPostExecute(String s) { 
           super.onPostExecute(s); 
          } 
         }.execute((LatLng) toPosition); 



private class GetRouteTask extends AsyncTask<LatLng, Void, String> { 

     String response = ""; 

     @Override 
     protected String doInBackground(LatLng... params) { 
      document = route.getDocument(fromPosition, params[0], GmapV2Direction.MODE_WALKING);//fromPosition is another LatLng object.can be your current location. 
      response = "Success"; 
      return response; 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      // directionMap.clear(); 

      if (response.equalsIgnoreCase("Success")) { 
       Log.v("Map", "got here"); 
       ArrayList<LatLng> directionPoint = route.getDirection(document); 
       PolylineOptions rectLine = new PolylineOptions().width(10).color(
         Color.RED); 

       for (int i = 0; i < directionPoint.size(); i++) { 
        rectLine.add(directionPoint.get(i)); 
       } 
       // Adding route on the map 

       map.addPolyline(rectLine); 
      } 

     } 
    }