0

In my application, I have added two places autocomplete fragments as source and destination search box for direction. I have added two markers to it but it only shows the location written in the box on the map. I tried adding polyline but it's not working. I have only added google maps, location and places API.

 PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);


        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                addMarker(place);
                // Log.i(TAG, "Place: " + place.getName());
                String placeName = place.getName().toString();
                place.getLatLng();


            }

            @Override
            public void onError(Status status) {                       
            }
        });

        PlaceAutocompleteFragment autocompleteFragments = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragments);
        autocompleteFragments.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place places) {                  
                addMarker(place);//used to add marker according to the given string                       
                String placeName = places.getName().toString();
                places.getLatLng();


            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                // Log.i(TAG, "An error occurred: " + status);

            }
        });
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;               
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {                

            return;
        }
        mMap.setMyLocationEnabled(true);
    }

    public void addMarker(Place p){
        MarkerOptions markerOptions=new MarkerOptions();
        markerOptions.position(p.getLatLng()).title(p.getName()+"");
        mMap.addMarker(markerOptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(p.getLatLng()));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(13));
        PolylineOptions polylineOptions=new PolylineOptions();

//tried to add polyline but did not worked

   polylineOptions.add(p.getLatLng()).width(5).color(Color.BLUE).geodesic(true);
        mMap.addPolyline(polylineOptions);

//p.getLatLng() this is used to get the latlng but i dont know how to get a polyline between them

    }
    } 
  • You have to call Google map diorection api follow this- -https://developers.google.com/maps/documentation/directions/intro – Adil Feb 07 '18 at 09:42

1 Answers1

0
ArrayList<LatLng> list = getArrayListOfLatLng();
PolyLine line;

PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
    for (int z = 0; z < list.size(); z++) {
        LatLng point = list.get(z);
        options.add(point);
    }
    line = myMap.addPolyline(options);
Tarun konda
  • 1,740
  • 1
  • 11
  • 19
  • I am getting error in it. I have not defined a list or line. What list should i add – Prajakta Thorat Feb 07 '18 at 09:48
  • 2
    Please include some commentary around your code. The code itself is not enough to provide an answer. – Dr Rob Lang Feb 07 '18 at 10:29
  • here line means PolyLine and list means LatLng ArrayList if it is helpful vote to me otherwise comment me i ll try to help u here is the link of polyline of gmaps https://stackoverflow.com/questions/17425499/how-to-draw-interactive-polyline-on-route-google-maps-v2-android – Tarun konda Feb 07 '18 at 11:06