Draw path between two points using Google Maps Android API v2

前端 未结 4 1117
日久生厌
日久生厌 2020-11-22 11:46

Google changed its map API for Android and introduced API V2. The previous codes for drawing path are not working with API V2.

I have managed to draw a path with API

4条回答
  •  北海茫月
    2020-11-22 12:24

    Dont know whether I should put this as answer or not...

    I used @Zeeshan0026's solution to draw the path...and the problem was that if I draw path once, and then I do try to draw path once again, both two paths show and this continues...paths showing even when markers were deleted... while, ideally, old paths' shouldn't be there once new path is drawn / markers are deleted..

    going through some other question over SO, I had the following solution

    I add the following function in Zeeshan's class

     public void clearRoute(){
    
             for(Polyline line1 : polylines)
             {
                 line1.remove();
             }
    
             polylines.clear();
    
         }
    

    in my map activity, before drawing the path, I called this function.. example usage as per my app is

    private Route rt;
    
    rt.clearRoute();
    
                if (src == null) {
                    Toast.makeText(getApplicationContext(), "Please select your Source", Toast.LENGTH_LONG).show();
                }else if (Destination == null) {
                    Toast.makeText(getApplicationContext(), "Please select your Destination", Toast.LENGTH_LONG).show();
                }else if (src.equals(Destination)) {
                    Toast.makeText(getApplicationContext(), "Source and Destinatin can not be the same..", Toast.LENGTH_LONG).show();
                }else{
    
                    rt.drawRoute(mMap, MapsMainActivity.this, src,
                            Destination, false, "en");
                }
    

    you can use rt.clearRoute(); as per your requirements.. Hoping that it will save a few minutes of someone else and will help some beginner in solving this issue..

    Complete Class Code

    see on github

    Edit: here is part of code from mainactivity..

    case R.id.mkrbtn_set_dest:
                        Destination = selmarker.getPosition();
                        destmarker = selmarker;
                        desShape = createRouteCircle(Destination, false);
    
                        if (src == null) {
                            Toast.makeText(getApplicationContext(),
                                    "Please select your Source first...",
                                    Toast.LENGTH_LONG).show();
                        } else if (src.equals(Destination)) {
                            Toast.makeText(getApplicationContext(),
                                    "Source and Destinatin can not be the same..",
                                    Toast.LENGTH_LONG).show();
                        } else {
    
                            if (isNetworkAvailable()) {
                                rt.drawRoute(mMap, MapsMainActivity.this, src,
                                        Destination, false, "en");
                                src = null;
                                Destination = null;
    
                            } else {
                                Toast.makeText(
                                        getApplicationContext(),
                                        "Internet Connection seems to be OFFLINE...!",
                                        Toast.LENGTH_LONG).show();
    
                            }
    
                        }
    
                        break;
    

    Edit 2 as per comments

    usage :

    //variables as data members
    GoogleMap mMap;
    private Route rt;
    static LatLng src;
    static LatLng Destination;
    //MapsMainActivity is my activity
    //false for interim stops for traffic, google
    // en language for html description returned
    
    rt.drawRoute(mMap, MapsMainActivity.this, src,
                                Destination, false, "en");
    

提交回复
热议问题