how to draw path between 2 points on google map

前端 未结 2 1791
既然无缘
既然无缘 2020-12-10 23:40

I have been working on an android application which uses Google map.Now I want to generate the path (driving direction) between 2 points on the map,how can this be done?

相关标签:
2条回答
  • 2020-12-11 00:09

    you can use the latest google api

    http://developer.android.com/google/play-services/maps.html

    thre are lot of links available for this. take a look on

    Drawing a line/path on Google Maps

    How to Draw Route in Google Maps API V2 from my location

    Android: How to draw route directions google maps API V2 from current location to destination

    0 讨论(0)
  • 2020-12-11 00:23
    public  void DrawLine(LatLng location){
        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.add(location)
        .add(new LatLng(mlatitude, mlongitude))
                .add(new LatLng(mlatitudeEnd,mlongitudeEND));
             mMap.addPolyline(polylineOptions);
    
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }
    
    @Override
    protected void onStop() {
        mGoogleApiClient.disconnect();
        super.onStop();
    }
    
    @Override
    public void onLocationChanged(Location location) {
        mLastLocation = location;     
    }
    
    
    protected void placeMarkerOnMap(LatLng location){
        MarkerOptions markerOptions=new MarkerOptions().position(location);
        String str_getloc = getAddress(location);
        markerOptions.title(str_getloc);
        mMap.addMarker(markerOptions);
    }
    private String getAddress(LatLng location){
        Geocoder geocoder=new Geocoder(this);
        String addresstxt="";
        List<Address> addresses=null;
        Address address=null;
    
        try {
            addresses=geocoder.getFromLocation(location.latitude,location.longitude,1);
            //addresstxt= String.valueOf((new LatLng(mlatitude,mlongitude)));
            //addresses.add(addresstxt)
            if (null != addresses && !addresses.isEmpty() ){
                address=addresses.get(0);
                for (int i=0; i<address.getMaxAddressLineIndex();i++){
                    addresstxt += (i==0) ?address.getAddressLine(i): ("\n"+address.getAddressLine(i));
    
                }
                if (mlatitudeEnd!=0.0&&mlongitudeEND!=0.0){
                    Toast.makeText(this, "if", Toast.LENGTH_SHORT).show();
                    // DrawLine(new LatLng(mlatitude,mlongitude));
                    DrawLine(location);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        return addresstxt;
    }
    
    0 讨论(0)
提交回复
热议问题