Android:如何绘制从当前位置到目的地的路线方向谷歌地图 API V2

Posted

技术标签:

【中文标题】Android:如何绘制从当前位置到目的地的路线方向谷歌地图 API V2【英文标题】:Android: How to draw route directions google maps API V2 from current location to destination 【发布时间】:2013-01-04 20:06:11 【问题描述】:

如何绘制从当前位置到目的地的路线方向(纬度和经度),我的代码如下:

import android.os.Bundle;
import com.actionbarsherlock.app.SherlockActivity;

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.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class DetailMapActivity extends SherlockActivity 
    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_detail_map);

        LatLng TO_DESTINATION = new LatLng(-6.33438, 106.74316);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
        map.setMyLocationEnabled(true);

        map.addMarker(new MarkerOptions().position(TO_DESTINATION).title("Destination Title")
                .snippet("Destination Description"));

        map.moveCamera(CameraUpdateFactory.newLatLngZoom(TO_LOCATION, 40));

        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    

我想通过供应商网络或gps从当前位置绘制,然后通过驾驶到目的地绘制路线。

我正在使用 android google apis V2。

感谢您的帮助。

【问题讨论】:

【参考方案1】:

这对我很有用:这不是我的代码,我从 *** 的一个很好的答案中得到它,但我现在找不到这个答案,所以这里是代码:

将此类添加到您的项目中:

package ...;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.google.android.gms.maps.model.LatLng;

import android.content.Context;
import android.util.Log;

public class GMapV2Direction 
    public final static String MODE_DRIVING = "driving";
    public final static String MODE_WALKING = "walking";

    public GMapV2Direction() 
    

    public Document getDocument(LatLng start, LatLng end, String mode) 
        String url = "http://maps.googleapis.com/maps/api/directions/xml?"
                + "origin=" + start.latitude + "," + start.longitude
                + "&destination=" + end.latitude + "," + end.longitude
                + "&sensor=false&units=metric&mode=driving";
        Log.d("url", url);
        try 
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse response = httpClient.execute(httpPost, localContext);
            InputStream in = response.getEntity().getContent();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            Document doc = builder.parse(in);
            return doc;
         catch (Exception e) 
            e.printStackTrace();
        
        return null;
    

    public String getDurationText(Document doc) 
        try 

            NodeList nl1 = doc.getElementsByTagName("duration");
            Node node1 = nl1.item(0);
            NodeList nl2 = node1.getChildNodes();
            Node node2 = nl2.item(getNodeIndex(nl2, "text"));
            Log.i("DurationText", node2.getTextContent());
            return node2.getTextContent();
         catch (Exception e) 
            return "0";
        
    

    public int getDurationValue(Document doc) 
        try 
            NodeList nl1 = doc.getElementsByTagName("duration");
            Node node1 = nl1.item(0);
            NodeList nl2 = node1.getChildNodes();
            Node node2 = nl2.item(getNodeIndex(nl2, "value"));
            Log.i("DurationValue", node2.getTextContent());
            return Integer.parseInt(node2.getTextContent());
         catch (Exception e) 
            return -1;
        
    

    public String getDistanceText(Document doc) 
        /*
         * while (en.hasMoreElements())  type type = (type) en.nextElement();
         * 
         * 
         */

        try 
            NodeList nl1;
            nl1 = doc.getElementsByTagName("distance");

            Node node1 = nl1.item(nl1.getLength() - 1);
            NodeList nl2 = null;
            nl2 = node1.getChildNodes();
            Node node2 = nl2.item(getNodeIndex(nl2, "value"));
            Log.d("DistanceText", node2.getTextContent());
            return node2.getTextContent();
         catch (Exception e) 
            return "-1";
        

        /*
         * NodeList nl1; if(doc.getElementsByTagName("distance")!=null) nl1=
         * doc.getElementsByTagName("distance");
         * 
         * Node node1 = nl1.item(nl1.getLength() - 1); NodeList nl2 = null; if
         * (node1.getChildNodes() != null)  nl2 = node1.getChildNodes(); Node
         * node2 = nl2.item(getNodeIndex(nl2, "value")); Log.d("DistanceText",
         * node2.getTextContent()); return node2.getTextContent();  else return
         * "-1"; else return "-1";
         */
    

    public int getDistanceValue(Document doc) 
        try 
            NodeList nl1 = doc.getElementsByTagName("distance");
            Node node1 = null;
            node1 = nl1.item(nl1.getLength() - 1);
            NodeList nl2 = node1.getChildNodes();
            Node node2 = nl2.item(getNodeIndex(nl2, "value"));
            Log.i("DistanceValue", node2.getTextContent());
            return Integer.parseInt(node2.getTextContent());
         catch (Exception e) 
            return -1;
        
        /*
         * NodeList nl1 = doc.getElementsByTagName("distance"); Node node1 =
         * null; if (nl1.getLength() > 0) node1 = nl1.item(nl1.getLength() - 1);
         * if (node1 != null)  NodeList nl2 = node1.getChildNodes(); Node node2
         * = nl2.item(getNodeIndex(nl2, "value")); Log.i("DistanceValue",
         * node2.getTextContent()); return
         * Integer.parseInt(node2.getTextContent());  else return 0;
         */
    

    public String getStartAddress(Document doc) 
        try 
            NodeList nl1 = doc.getElementsByTagName("start_address");
            Node node1 = nl1.item(0);
            Log.i("StartAddress", node1.getTextContent());
            return node1.getTextContent();
         catch (Exception e) 
            return "-1";
        

    

    public String getEndAddress(Document doc) 
        try 
            NodeList nl1 = doc.getElementsByTagName("end_address");
            Node node1 = nl1.item(0);
            Log.i("StartAddress", node1.getTextContent());
            return node1.getTextContent();
         catch (Exception e) 
            return "-1";        
    
    
    public String getCopyRights(Document doc) 
        try 
            NodeList nl1 = doc.getElementsByTagName("copyrights");
            Node node1 = nl1.item(0);
            Log.i("CopyRights", node1.getTextContent());
            return node1.getTextContent();
         catch (Exception e) 
        return "-1";
        

    

    public ArrayList<LatLng> getDirection(Document doc) 
        NodeList nl1, nl2, nl3;
        ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();
        nl1 = doc.getElementsByTagName("step");
        if (nl1.getLength() > 0) 
            for (int i = 0; i < nl1.getLength(); i++) 
                Node node1 = nl1.item(i);
                nl2 = node1.getChildNodes();

                Node locationNode = nl2
                        .item(getNodeIndex(nl2, "start_location"));
                nl3 = locationNode.getChildNodes();
                Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
                double lat = Double.parseDouble(latNode.getTextContent());
                Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
                double lng = Double.parseDouble(lngNode.getTextContent());
                listGeopoints.add(new LatLng(lat, lng));

                locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
                nl3 = locationNode.getChildNodes();
                latNode = nl3.item(getNodeIndex(nl3, "points"));
                ArrayList<LatLng> arr = decodePoly(latNode.getTextContent());
                for (int j = 0; j < arr.size(); j++) 
                    listGeopoints.add(new LatLng(arr.get(j).latitude, arr
                            .get(j).longitude));
                

                locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
                nl3 = locationNode.getChildNodes();
                latNode = nl3.item(getNodeIndex(nl3, "lat"));
                lat = Double.parseDouble(latNode.getTextContent());
                lngNode = nl3.item(getNodeIndex(nl3, "lng"));
                lng = Double.parseDouble(lngNode.getTextContent());
                listGeopoints.add(new LatLng(lat, lng));
            
        

        return listGeopoints;
    

    private int getNodeIndex(NodeList nl, String nodename) 
        for (int i = 0; i < nl.getLength(); i++) 
            if (nl.item(i).getNodeName().equals(nodename))
                return i;
        
        return -1;
    

    private ArrayList<LatLng> decodePoly(String encoded) 
        ArrayList<LatLng> poly = new ArrayList<LatLng>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;
        while (index < len) 
            int b, shift = 0, result = 0;
            do 
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
             while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;
            shift = 0;
            result = 0;
            do 
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
             while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            LatLng position = new LatLng((double) lat / 1E5, (double) lng / 1E5);
            poly.add(position);
        
        return poly;
    

然后根据您的需要使用这个类。 例如绘制方向:

GMapV2Direction md = new GMapV2Direction();
mMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
Document doc = md.getDocument(sourcePosition, destPosition,
                    GMapV2Direction.MODE_DRIVING);

ArrayList<LatLng> directionPoint = md.getDirection(doc);
            PolylineOptions rectLine = new PolylineOptions().width(3).color(
                    Color.RED);

            for (int i = 0; i < directionPoint.size(); i++) 
                rectLine.add(directionPoint.get(i));
            
            Polyline polylin = mMap.addPolyline(rectLine);

sourcePosition, destPosition 来自 LatLng 类型,你给他们想要的分数。

我在这里写了我认为可以提供帮助的代码部分,欢迎提出任何问题。

【讨论】:

想找原帖的可以看看***.com/a/15053901/1468354 这是旧的,但这段代码在主线程上运行网络。只是为了提醒人们。 +1也用于提供来源。一段很棒的代码。 谷歌有什么限制吗?例如请求数量? 使用此 api 有什么限制或限制吗? @dvrm 您实际上是在为这个解决方案使用 XML 解析?没有 RESTful 服务吗?【参考方案2】:

使用 dvrm 解决方案 GMapV2Direction 类,这是一个很好的解决方案,它仍然有效,但现在您需要在新线程中实现 getDocument 方法以允许网络连接。这是此方法的 AsyncTask 示例,该类的其余部分相同。您可以使用 Handle 处理它或实现自己的接口回调以获取文档 xml 响应。

public class GMapV2DirectionAsyncTask extends AsyncTask<String, Void, Document> 

private final static String TAG = GMapV2DirectionAsyncTask.class.getSimpleName();
private Handler handler;
private LatLng  start, end;
private String mode;

public GMapV2DirectionAsyncTask(Handler handler, LatLng start, LatLng end, String mode) 
    this.start = start;
    this.end = end;
    this.mode = mode;
    this.handler = handler;


@Override
protected Document doInBackground(String... params) 

    String url = "http://maps.googleapis.com/maps/api/directions/xml?"
            + "origin=" + start.latitude + "," + start.longitude
            + "&destination=" + end.latitude + "," + end.longitude
            + "&sensor=false&units=metric&mode=" + mode;
    Log.d("url", url);
    try 
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response = httpClient.execute(httpPost, localContext);
        InputStream in = response.getEntity().getContent();
        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        Document doc = builder.parse(in);
        return doc;
     catch (Exception e) 
        e.printStackTrace();
    
    return null;


@Override
protected void onPostExecute(Document result) 
    if (result != null) 
        Log.d(TAG, "---- GMapV2DirectionAsyncTask OK ----");
        Message message = new Message();
        message.obj = result;
        handler.dispatchMessage(message);
     else 
        Log.d(TAG, "---- GMapV2DirectionAsyncTask ERROR ----");
    


@Override
protected void onPreExecute() 


@Override
protected void onProgressUpdate(Void... values) 

这是活动的异步处理方法:

    protected void route(LatLng sourcePosition, LatLng destPosition, String mode) 
    final Handler handler = new Handler() 
        public void handleMessage(Message msg) 
            try 
                Document doc = (Document) msg.obj;
                GMapV2Direction md = new GMapV2Direction();
                ArrayList<LatLng> directionPoint = md.getDirection(doc);
                PolylineOptions rectLine = new PolylineOptions().width(15).color(getActivity().getResources().getColor(R.color.magoo_user_base_color));

                for (int i = 0; i < directionPoint.size(); i++) 
                    rectLine.add(directionPoint.get(i));
                
                Polyline polylin = googleMap.addPolyline(rectLine);
                md.getDurationText(doc);
             catch (Exception e) 
                e.printStackTrace();
            
        

    ;

    new GMapV2DirectionAsyncTask(handler, sourcePosition, destPosition, GMapV2Direction.MODE_DRIVING).execute();

还有一点,如果duration值不对,解决方法是修改GMapV2Direction类的一行代码:

Node node1 = nl1.item(nl1.getLength() - 1);

改为:

Node node1 = nl1.item(0); 

【讨论】:

【参考方案3】:

我知道为时已晚,但值得。我已经搜索并尝试了很多示例,但没有得到想要的结果,所以我找到了这个惊人的库,它可以为您提供想要的结果。你必须写一堆线。

链接:https://github.com/jd-alexander/Google-Directions-Android

导入库:

实现'com.github.jd-alexander:library:1.1.0'

我正在 onMarkerClick 中实现代码,所以如果用户点击标记,那么它将从他当前的位置路由到标记位置,但您可以根据您的要求进行更改。

 @Override
    public boolean onMarkerClick(Marker marker) 
        Routing routing = new Routing.Builder()
                .travelMode(AbstractRouting.TravelMode.DRIVING)
                .withListener(MapsActivity.this)
                .waypoints(new LatLng(latitude, longitude), marker.getPosition())
                .key("AIzaSyDGvkmJ6osdJz7v8OSg7U")
                .build();
        routing.execute();
        return false;
    

实现路由监听器

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, RoutingListener

覆盖方法

   @Override
    public void onRoutingFailure(RouteException e) 
        Log.e("check", e.getMessage());
    

    @Override
    public void onRoutingStart() 
        Log.e("check", "onRoutingStart");
    

    @Override
    public void onRoutingSuccess(ArrayList<Route> route, int shortestRouteIndex) 

        Log.e("check", "onRoutingSuccess");
        CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude,longitude));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(16);
        List<Polyline> polylines = new ArrayList<>();

        mMap.moveCamera(center);


        if (polylines.size() > 0) 
            for (Polyline poly : polylines) 
                poly.remove();
            
        

        polylines = new ArrayList<>();
        //add route(s) to the map.
        for (int i = 0; i < route.size(); i++) 

            //In case of more than 5 alternative routes

            PolylineOptions polyOptions = new PolylineOptions();
            polyOptions.color(getResources().getColor(R.color.colorRouteLine));
            polyOptions.width(10 + i * 3);
            polyOptions.addAll(route.get(i).getPoints());
            Polyline polyline = mMap.addPolyline(polyOptions);
            polylines.add(polyline);
        
    

    @Override
    public void onRoutingCancelled() 
        Log.e("check", "onRoutingCancelled");
    

这里是示例项目,您也可以查看此代码。

https://github.com/jd-alexander/Google-Directions-Android/blob/master/sample/src/main/java/com/directions/sample/MainActivity.java

结果

【讨论】:

【参考方案4】:

在这里,我发布了用于在当前位置和谷歌地图上的另一个地方之间绘制路线的完整代码。

更多详情:Visit draw route on google map with current location

PointParser 类

import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PointsParser extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> 

    TaskLoadedCallback taskCallback;
    String directionMode = "driving";

    public PointsParser(Context mContext, String directionMode) 
        this.taskCallback = (TaskLoadedCallback) mContext;
        this.directionMode = directionMode;
    

    // Parsing the data in non-ui thread
    @Override
    protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) 

        JSONObject jObject;
        List<List<HashMap<String, String>>> routes = null;

        try 
            jObject = new JSONObject(jsonData[0]);
            Log.d("mylog", jsonData[0].toString());
            DataParser parser = new DataParser();
            Log.d("mylog", parser.toString());

            // Starts parsing data
            routes = parser.parse(jObject);
            Log.d("mylog", "Executing routes");
            Log.d("mylog", routes.toString());

         catch (Exception e) 
            Log.d("mylog", e.toString());
            e.printStackTrace();
        
        return routes;
    

    // Executes in UI thread, after the parsing process
    @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result) 
        ArrayList<LatLng> points;
        PolylineOptions lineOptions = null;
        // Traversing through all the routes
        for (int i = 0; i < result.size(); i++) 
            points = new ArrayList<>();
            lineOptions = new PolylineOptions();
            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);
            // Fetching all the points in i-th route
            for (int j = 0; j < path.size(); j++) 
                HashMap<String, String> point = path.get(j);
                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng);
                points.add(position);
            
            // Adding all the points in the route to LineOptions
            lineOptions.addAll(points);
            if (directionMode.equalsIgnoreCase("walking")) 
                lineOptions.width(10);
                lineOptions.color(Color.MAGENTA);
             else 
                lineOptions.width(20);
                lineOptions.color(Color.RED);
            
            Log.d("mylog", "onPostExecute lineoptions decoded");
        

        // Drawing polyline in the Google Map for the i-th route
        if (lineOptions != null) 
            //mMap.addPolyline(lineOptions);
            taskCallback.onTaskDone(lineOptions);

         else 
            Log.d("mylog", "without Polylines drawn");
        
    

FetchURL 类

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class FetchURL extends AsyncTask<String, Void, String> 

    Context mContext;
    String directionMode = "driving";

    public FetchURL(Context mContext) 
        this.mContext = mContext;
    

    @Override
    protected String doInBackground(String... strings) 
        // For storing data from web service
        String data = "";
        directionMode = strings[1];
        try 
            // Fetching the data from web service
            data = downloadUrl(strings[0]);
            Log.d("mylog", "Background task data " + data.toString());
         catch (Exception e) 
            Log.d("Background Task", e.toString());
        
        return data;
    

    @Override
    protected void onPostExecute(String s) 
        super.onPostExecute(s);
        PointsParser parserTask = new PointsParser(mContext, directionMode);
        // Invokes the thread for parsing the JSON data
        parserTask.execute(s);
    

    private String downloadUrl(String strUrl) throws IOException 
        String data = "";
        InputStream iStream = null;
        HttpURLConnection urlConnection = null;
        try 
            URL url = new URL(strUrl);
            // Creating an http connection to communicate with url
            urlConnection = (HttpURLConnection) url.openConnection();
            // Connecting to url
            urlConnection.connect();
            // Reading data from url
            iStream = urlConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
            StringBuffer sb = new StringBuffer();
            String line = "";
            while ((line = br.readLine()) != null) 
                sb.append(line);
            
            data = sb.toString();
            Log.d("mylog", "Downloaded URL: " + data.toString());
            br.close();
         catch (Exception e) 
            Log.d("mylog", "Exception downloading URL: " + e.toString());
         finally 
            iStream.close();
            urlConnection.disconnect();
        
        return data;
    

主要活动

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener , OnMapReadyCallback, TaskLoadedCallback

   //variables for map and route

    private GoogleMap mMap;
    private MarkerOptions place1, place2;
    Button getDirection;
    private Polyline currentPolyline;
    private MapFragment mapFragment;
    private boolean isFirstTime = true;

//variables for current location
    private static final String TAG = "MainActivity";

    private TextView tvLocation;
    private GoogleApiClient mGoogleApiClient;
    private Location mLocation;
    private LocationRequest mLocationRequest;
    private com.google.android.gms.location.LocationListener listener;
    private long UPDATE_INTERVAL = 2 * 1000;  /* 10 secs */
    private long FASTEST_INTERVAL = 2000; /* 2 sec */

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //code for getting current location
        requestMultiplePermissions();

        tvLocation = (TextView) findViewById((R.id.tv));

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();


    

    //code for drawing route

    @Override
    public void onMapReady(GoogleMap googleMap) 
        mMap = googleMap;
        mMap.clear();
        Log.d("mylog", "Added Markers");
        mMap.addMarker(place1);
        mMap.addMarker(place2);

        CameraPosition googlePlex = CameraPosition.builder()
                .target(new LatLng(22.7739,71.6673))
                .zoom(7)
                .bearing(0)
                .tilt(45)
                .build();

        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(googlePlex), 5000, null);
    

    private String getUrl(LatLng origin, LatLng dest, String directionMode) 
        // Origin of route
        String str_origin = "origin=" + origin.latitude + "," + origin.longitude;
        // Destination of route
        String str_dest = "destination=" + dest.latitude + "," + dest.longitude;
        // Mode
        String mode = "mode=" + directionMode;
        // Building the parameters to the web service
        String parameters = str_origin + "&" + str_dest + "&" + mode;
        // Output format
        String output = "json";
        // Building the url to the web service
        String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters + "&key=" + getString(R.string.google_maps_key);
        return url;
    

    @Override
    public void onTaskDone(Object... values) 
        if (currentPolyline != null)
            currentPolyline.remove();
        currentPolyline = mMap.addPolyline((PolylineOptions) values[0]);
    

    //runtime permission method

    private void  requestMultiplePermissions()
        Dexter.withActivity(this)
                .withPermissions(
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION )
                .withListener(new MultiplePermissionsListener() 
                    @Override
                    public void onPermissionsChecked(MultiplePermissionsReport report) 
                        // check if all permissions are granted
                        if (report.areAllPermissionsGranted()) 
                            Toast.makeText(getApplicationContext(), "All permissions are granted by user!", Toast.LENGTH_SHORT).show();
                        

                        // check for permanent denial of any permission
                        if (report.isAnyPermissionPermanentlyDenied()) 
                            // show alert dialog navigating to Settings
                            openSettingsDialog();
                        
                    

                    @Override
                    public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) 
                        token.continuePermissionRequest();
                    
                ).
                withErrorListener(new PermissionRequestErrorListener() 
                    @Override
                    public void onError(DexterError error) 
                        Toast.makeText(getApplicationContext(), "Some Error! ", Toast.LENGTH_SHORT).show();
                    
                )
                .onSameThread()
                .check();
    

    private void openSettingsDialog() 

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Required Permissions");
        builder.setMessage("This app require permission to use awesome feature. Grant them in app settings.");
        builder.setPositiveButton("Take Me To SETTINGS", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                dialog.cancel();
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivityForResult(intent, 101);
            
        );
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                dialog.cancel();
            
        );
        builder.show();

    


   //methods for getting current location

    @Override
    public void onConnected(Bundle bundle) 
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        

        startLocationUpdates();

        mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

        if(mLocation == null)
            startLocationUpdates();
        
        if (mLocation != null) 

            // mLatitudeTextView.setText(String.valueOf(mLocation.getLatitude()));
            //mLongitudeTextView.setText(String.valueOf(mLocation.getLongitude()));
         else 
            Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
        
    

    @Override
    public void onConnectionSuspended(int i) 
        Log.i(TAG, "Connection Suspended");
        mGoogleApiClient.connect();
    

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) 
        Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode());
    

    @Override
    protected void onStart() 
        super.onStart();
        if (mGoogleApiClient != null) 
            mGoogleApiClient.connect();
        
    

    @Override
    protected void onStop() 
        super.onStop();
        if (mGoogleApiClient.isConnected()) 
            mGoogleApiClient.disconnect();
        
    

    protected void startLocationUpdates() 
        // Create the location request
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(UPDATE_INTERVAL)
                .setFastestInterval(FASTEST_INTERVAL);
        // Request location updates
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                mLocationRequest, this);
        Log.d("reque", "--->>>>");
    

    @Override
    public void onLocationChanged(Location location) 

        String msg = "Updated Location: " +
                Double.toString(location.getLatitude()) + "," +
                Double.toString(location.getLongitude());

        tvLocation.setText(String.valueOf(location.getLatitude() +"    "+String.valueOf(location.getLongitude())));
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();

        if(isFirstTime)
            //code to draw path on map

            getDirection = findViewById(R.id.btnGetDirection);
            getDirection.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View view) 
                    new FetchURL(MainActivity.this).execute(getUrl(place1.getPosition(), place2.getPosition(), "driving"), "driving");
                
            );

            place1 = new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Location 1");
            place2 = new MarkerOptions().position(new LatLng(20.8880, 70.4012)).title("Location 2");
            mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapNearBy);
            mapFragment.getMapAsync(this);
            isFirstTime = false;
        

     


如果您有任何错误,请查看此答案开头给出的原始教程。

【讨论】:

【参考方案5】:
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_detail_map);
    .....
    .....

  /* Use the LocationManager class to obtain GPS locations */
  LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

  LocationListener mlocListener = new MyLocationListener();
  mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);


/* Class My Location Listener */
public class MyLocationListener implements LocationListener


  @Override
  public void onLocationChanged(Location loc)
  
   CURRENT_LAT = loc.getLatitude();
   CURRENT_LNG = loc.getLongitude();
  

  @Override
  public void onProviderDisabled(String provider)
  
    Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
  

  @Override
  public void onProviderEnabled(String provider)
  
    Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
  

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras)
  

  

【讨论】:

感谢 Gaurav 的回复,我已经解决了获取当前位置的问题,只需添加这一行 map.setMyLocationEnabled(true);现在如何通过从当前位置开车到位置目的地来绘制方向。谢谢。

以上是关于Android:如何绘制从当前位置到目的地的路线方向谷歌地图 API V2的主要内容,如果未能解决你的问题,请参考以下文章

如何绘制从我的位置到目的地位置的路线?

Android- Google Maps V2:跟踪从当前位置到另一个目的地的路线

Mapbox Android:如何获取从当前位置到您选择的目的地的路线?

在iOS App中绘制用户当前位置到目的地之间的路线方向? [复制]

如何在手机 SDK 中将路线图源绘制到目的地?

从当前位置到 poi android maps v2 的路线